Recently, I started to learn a new programming language, rust. It has an interesting feature called move and borrow, but it is really hard to understand in some situations.
To my opinion, if a variable prefixed with a &
, it's borrow, but the
following code confused me.
fn iter(lst: &[String]) {
//item variable is prefix with '&', so i think it's borrow, but the compiler tells me it's move.
//the compiler suggests to remove '&', completely contrary to what I thought.
for &item in lst {
println!("item is: {}", item)
}
}
Is there a intuitive way to distinguish the move and borrow.