I am not able to understand why following code will not compile because of movement during pattern matching:
struct Node<T> {
data: Option<T>
}
impl<T> Node<T> {
pub fn print_if_present(&self) -> () {
match self.data {
None => println!("Nothing present."),
Some(v) => println!("Something is there.")
}
}
}
let n = Node::<i32>{ data: Some(99) };
n.print_if_present();
}
The line Some(v) => println!("Something is there.") causes following compilation failure:
--> src/main.rs:8:19
|
8 | match self.data {
| ^^^^ cannot move out of borrowed content
9 | None => println!("Nothing present."),
10 | Some(v) => println!("Something is there.")
| - hint: to prevent move, use `ref v` or `ref mut v`
Where is the movement of values happening here?
However Some(_) => println!("Something is there.") builds and runs successfully. I understand that _ is unused value and hence prevents movement.
Why ref v would solve it and why &v wouldn't help?