Im building a data structure containing nodes, and each node might point to another. The pointers between the nodes are implemented using Rc, something like this:
struct Node {
ptr: Rc<Node>
}
I would like to be able to change the pointer 'ptr' of a node to point to another node, by cloning another existing Rc.
let a: Rc<Node> = ...;
let mut b: Node = ...;
let b.ptr = a.clone();
My problem is, the compiler think I am trying to set the value of the node, namely changing the underlying shared object of b.ptr, where I realy want to replace the pointer: reduce the refcount of the old value of b.ptr, increase the refcount of a, and change b.ptr to point to a.
I managed to do it with Cell<Rc>, but I seems too vebose and unnecessary.
How can I do that?
That's rather unlikely given the code you posted, and with code which is incomplete, separately broken, and without the error message, it's difficult to diagnose. Even Shepmaster has yet to achieve psychic powers letting him know information not provided.
Fixing the obviously missing or incorrect I get something like:
and the error message is completely clear:
this is fixed by simply declaring
bas mutable as the compiler suggests[0]:[0]: not that it's always right, far from it, but in this case its suggestion is fine.