push and item to vector rust code example
Example 1: rust empty vector
let mut vec = Vec::new();
Example 2: rust push to vec
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec.len(), 2);
assert_eq!(vec[0], 1);
assert_eq!(vec.pop(), Some(2));
assert_eq!(vec.len(), 1);
vec[0] = 7;
assert_eq!(vec[0], 7);
vec.extend([1, 2, 3].iter().copied());
for x in &vec {
println!("{}", x);
}
assert_eq!(vec, [7, 1, 2, 3]);
Example 3: push and item to vector rust
let mut vec = vec![10];
vec.push(100);
assert_eq!(vec[1], 100);