let v = vec![1, 2, 3, 4, 5, 6]; // v: Vec<i32>
is the type, Vec<T>
which means Vectors can be of any type (Generic Data Types (T))
the brackets are actually a convention, so you could technically use ()
too.
you can access vectors like normal arrays: v[1];
#![allow(unused_variables)]
fn main() {
let mut v = vec![1, 2, 3, 4, 5];
for i in &v {
println!("A reference to {}", i);
}
for i in &mut v {
println!("A mutable reference to {}", i);
}
for i in v {
println!("Take ownership of the vector and its element {}", i);
}
}
the last vector iteration would not work if you did it before the firs two because you are taking Ownership of the vector and it would be dropped from memory once the for
loop is out-of-scope.
the first two you can repeat over and over.