Vectors
Vec<T> is a growable list. Use push, pop, insert, remove, len, contains, sort, dedup and iter. Index with [] or get, which returns an Option.
Example
let mut v = vec![3, 1, 2];
v.push(4);
v.sort();
println!("{:?} {:?}", v, v.get(10));
println!("{} {}", v.len(), v.contains(&2));
v.retain(|&x| x != 3);
println!("{:?}", v);Result
[1, 2, 3, 4] None 4 true [1, 2, 4]
Exercise
Add an item.
v.(5)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.