I saw cannot assign to immutable field `curr.right`


Hi guys,
I am learning CIS 198 2016 fall course and working on the homework 02. I finished part of it but saw compiling error "cannot assign to immutable field curr.right"

I did some search but failed to solve it yet. You could find my code at https://play.rust-lang.org/?gist=12a7963f055b6cc5c8ae09c34b24e2c9&version=stable&backtrace=0

Any tips are appropriated.




I think the problem is here:

            Link::More(ref node) => {
                let mut curr = node;

curr is not a &mut Node here, it is a mutable variable which holds & Node. That is, you can make curr to point to another Node, but you can’t change the Node itself. You want to bind node with ref mut. If you know C, this is the difference between a constant pointer (char * const), a pointer to constant (char const *) and a constant pointer to constant (char const * const).


I think what you should do here is to factor out contents of the loop {} into a function that takes a &mut Node and a value.


Hi matklad,
Yes, I also feel confusion about the mut usage though I have 10+ years experience with C and C++.


Hi birkenfeld,

I tried to factor out the loop, but still struggled with a mutability issue. You could check my code here: https://play.rust-lang.org/?gist=b8ca61ebd24fec485815e5eae69184a6&version=nightly&backtrace=0


Ah, sorry, I was too terse. What I meant was that when you have factored the loop out, you can call the function recursively, instead of reassigning curr.


Hi birkenfeld,

OK.I will try.