Rust, unlike C++, does not have function overloading. Consider a linked list model that, in C++, would look like:
template <typename T>
class Node {
private:
T value;
std::unique_ptr<Node<T>> next;
public:
Node<T>& getNext();
const Node<T>& getNext() const;
};
When you deal with a constant list, you cannot modify it because you can only have access to const Node<T>&
, at the same time you can modify a non constant list.
What is the equivalent of this behaviour in Rust? Consider a more complex example with multiple methods. Only some of them must be overloaded with both &mut self
and &self
.
pub struct Node<T> {
value: T,
next: Option<Box<Node<T>>>,
}
impl<T> Node<T> {
pub fn getNext(&self) -> Option<&Self> {}
pub fn getNext(&mut self) -> Option<&mut Self> {}
}