When I read the Rust documentation on generics, I found a weird question about ownership. Looking at the following snippet:
fn main() {
let number_list = vec![34, 50, 25, 100, 65];
let mut largest = number_list[0];
for number in number_list {
if number > largest {
largest = number;
}
}
println!("The largest number is {}", largest);
}
Why doesn't largest
take ownership of the first element of vector? I found that in the previous chapter about vectors, using the borrow syntax let largest = &v[0]
would take ownership of the element. Why does this code pass the compiler?