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 Rust Playground

Any tips are appropriated.

Thanks.

Yanan

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++.
Thanks.
Yanan

Hi birkenfeld,

I tried to factor out the loop, but still struggled with a mutability issue. You could check my code here: Rust Playground

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.

Thanks.

Yanan