Hi. I'm teaching myself Rust from the book and experimenting a little as I go. I'm quite familiar with basic general imperative and functional programming concepts but I have very little practical programming experience.
In the code example below I'm confused as why I have to use a borrow operator (&) in the match arm Some(&first) => first,
from the first match block but not in Some(sixth) => println!("The sixth element is: {}", sixth),
from the second match block.
Does it have somethign to do with the fact that I'm assigning the result of the match to something in the first case, and not in the second?
Or is it to do with the println! macro automatically dereferencing?
Or am I barking up the wrong tree entirely?
I would greatly appreciate if someone could help to explain, please? Thanks
let mut v: Vec<i32> = vec![1, 2, 3, 4, 5];
let first: i32 = match v.get(0) {
Some(&first) => first,
None => 0,
};
println!("The first element is: {}", first);
v.push(6);
match v.get(5) {
Some(sixth) => println!("The sixth element is: {}", sixth),
None => println!("There is no sixth element."),
}