number_list is a list of integers, and integers implement the Copy trait, which means that they are never moved, but are copied instead. For this reason the array keeps ownership of its elements, and largest and number get copies of these elements.
If you try to do the same with a non-copy type, then you get an error:
struct WrappedInt(i32);
fn main() {
let number_list = vec![34, 50, 25, 100, 65]
.into_iter()
.map(|x| WrappedInt(x))
.collect::<Vec<_>>();
let mut largest = number_list[0];
for number in number_list {
if number.0 > largest.0 {
largest = number;
}
}
println!("The largest number is {}", largest.0);
assert_eq!(largest.0, 100);
}
playground
gives:
error[E0507]: cannot move out of index of `std::vec::Vec<WrappedInt>`
--> src/main.rs:9:23
|
9 | let mut largest = number_list[0];
| ^^^^^^^^^^^^^^
| |
| move occurs because value has type `WrappedInt`, which does not implement the `Copy` trait
| help: consider borrowing here: `&number_list[0]`