Why this fails Playground link
#[derive(Debug, Clone)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(value: i32) -> Self {
Node { value, next: None }
}
}
fn main() {
let mut head = &mut Box::new(Node::new(1));
let next_node = Box::new(Node::new(2));
let next_next_node = Box::new(Node::new(3));
head.next = Some(next_node);
match &mut head.next {
Some(next) => head = next,
None => {}
};
head.next = Some(next_next_node);
println!("Curr ->{:#?}", head);
}
Above ends up with an error
Compiling playground v0.0.1 (/playground)
error[E0506]: cannot assign to `head.next` because it is borrowed
--> src/main.rs:26:5
|
21 | match &mut head.next {
| -------------- `head.next` is borrowed here
...
26 | head.next = Some(next_next_node);
| ^^^^^^^^^
| |
| `head.next` is assigned to here but it was already borrowed
| borrow later used here
For more information about this error, try `rustc --explain E0506`.
error: could not compile `playground` (bin "playground") due to 1 previous error
From the error message, not sure if I understand it right, I get that next is borrowed as mutable, but it's moved to curr and later being used again like what happens in next example.
But why compiler isn't happy about same in the above example
While this works Playground link
#[derive(Debug, Clone)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(value: i32) -> Self {
Node { value, next: None }
}
}
fn main() {
let mut head = &mut Box::new(Node::new(1));
let next_node = Box::new(Node::new(2));
let next_next_node = Box::new(Node::new(3));
head.next = Some(next_node);
let next = head.next.as_mut().unwrap();
head = next;
head.next = Some(next_next_node);
println!("Curr ->{:#?}", head);
}
And this too works when ref is used Playground link
#[derive(Debug, Clone)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
impl Node {
fn new(value: i32) -> Self {
Node { value, next: None }
}
}
fn main() {
let mut head = &mut Box::new(Node::new(1));
let next_node = Box::new(Node::new(2));
let next_next_node = Box::new(Node::new(3));
head.next = Some(next_node);
match head.next {
Some(ref mut next) => head = next,
None => {}
};
head.next = Some(next_next_node);
println!("Curr ->{:#?}", head);
}
Thank you