Answers for "push and item to vector rust"

1

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]);
Posted by: Guest on September-23-2020
1

rust empty vector

let mut vec = Vec::new();
Posted by: Guest on September-09-2020
1

push and item to vector rust

let mut vec = vec![10];
vec.push(100);
assert_eq!(vec[1], 100);
Posted by: Guest on October-14-2020

Browse Popular Code Answers by Language