What does this print?
fn main() {
let mut v: Vec<i32> = Vec::new();
v.push(10);
v.push(20);
println!("{v:?}");
}Running it prints:
[10, 20]
Practice 08
Read each program and pick an answer. Some programs do not compile, and that can be the right answer. Your choice is checked as soon as you pick it, and saved in this browser.Need a refresher? Read lesson 8.
0 of 50 answered·0 correct
What does this print?
fn main() {
let mut v: Vec<i32> = Vec::new();
v.push(10);
v.push(20);
println!("{v:?}");
}Running it prints:
[10, 20]
What does this print?
fn main() {
let mut v: Vec<i32> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println!("{}", v.len());
}Running it prints:
3
What does this print?
fn main() {
let v = vec![5, 10, 15];
println!("{v:?}");
}Running it prints:
[5, 10, 15]
What does this print?
fn main() {
let v = vec![7; 3];
println!("{v:?}");
}Running it prints:
[7, 7, 7]
Does this compile?
fn main() {
let v = Vec::new();
println!("{v:?}");
}The compiler says:
error[E0282]: type annotations needed for `Vec<_>`
What does this print?
fn main() {
let v: Vec<i32> = Vec::new();
println!("{v:?}");
println!("{}", v.is_empty());
}Running it prints:
[] true
The numbers may differ on your machine, so write what your reasoning says, not what you remember from the lesson.
fn main() {
let mut v: Vec<i32> = Vec::new();
v.push(1);
println!("{}", v.capacity());
v.push(2);
v.push(3);
v.push(4);
println!("{}", v.capacity());
v.push(5);
println!("{}", v.capacity());
}Running it prints:
4 4 8
vec! already knows how many items it needs room for.
fn main() {
let v = vec![1, 2, 3];
println!("{} {}", v.len(), v.capacity());
}Running it prints:
3 3
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
v.push(4);
let cap_before = v.capacity();
v.clear();
println!("{} {}", v.len(), v.capacity());
println!("{}", v.capacity() == cap_before);
}Running it prints:
0 6 true
What does this print?
fn main() {
let mut v = vec![1, 2, 3, 4];
v.remove(0);
println!("{} {}", v.len(), v.capacity());
}Running it prints:
3 4
What does this print?
fn main() {
let mut v: Vec<i32> = Vec::new();
for i in 1..=6 {
v.push(i);
println!("{} {}", v.len(), v.capacity());
}
}Running it prints:
1 4 2 4 3 4 4 4 5 8 6 8
What does this print?
fn main() {
let v = vec![10, 20, 30, 40];
println!("{}", v[0]);
println!("{}", v[v.len() - 1]);
}Running it prints:
10 40
What does this print?
fn main() {
let v = vec![1, 2, 3];
let i = v.len();
println!("{}", v[i]);
}Running it prints nothing.
Then it panics: index out of bounds: the len is 3 but the index is 3
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
v[1] = 99;
println!("{v:?}");
}Running it prints:
[1, 99, 3]
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
v.insert(0, 100);
println!("{v:?}");
}Running it prints:
[100, 1, 2, 3]
What does this print?
fn main() {
let mut v = vec![5, 10, 15, 20];
let taken = v.remove(1);
println!("{taken} {v:?}");
}Running it prints:
10 [5, 15, 20]
What does this print?
fn main() {
let mut v = vec![4, 1, 3, 2];
v.sort();
println!("{v:?}");
v.reverse();
println!("{v:?}");
}Running it prints:
[1, 2, 3, 4] [4, 3, 2, 1]
What does this print?
fn main() {
let v = vec![2, 4, 6, 8];
println!("{}", v.contains(&6));
println!("{}", v.contains(&5));
}Running it prints:
true false
Does this compile?
fn main() {
let v = vec![2, 4, 6, 8];
println!("{}", v.contains(6));
}The compiler says:
error[E0308]: mismatched types
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
v.clear();
println!("{} {}", v.len(), v.is_empty());
}Running it prints:
0 true
What does this print?
fn main() {
let v = vec![3, 6, 9];
for x in &v {
println!("{x}");
}
}Running it prints:
3 6 9
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
for x in &mut v {
*x *= 10;
}
println!("{v:?}");
}Running it prints:
[10, 20, 30]
Does this compile?
fn main() {
let mut v = vec![1, 2, 3];
for x in &mut v {
x *= 10;
}
println!("{v:?}");
}The compiler says:
error[E0368]: binary assignment operation `*=` cannot be applied to type `&mut {integer}`What does this print?
fn main() {
let v = vec!["a", "b", "c"];
for (i, s) in v.iter().enumerate() {
println!("{i}: {s}");
}
}Running it prints:
0: a 1: b 2: c
What does this print?
fn main() {
let v = vec![4, 5, 6];
let total: i32 = v.iter().sum();
println!("{total}");
}Running it prints:
15
What does this print?
fn main() {
let v = vec![3, 8, 1, 9, 4];
let mut smallest = v[0];
for x in &v {
if *x < smallest {
smallest = *x;
}
}
println!("{smallest}");
}Running it prints:
1
Does this compile?
fn main() {
let v = vec![1, 2, 3];
for x in v {
println!("{x}");
}
println!("{}", v.len());
}The compiler says:
error[E0382]: borrow of moved value: `v`
What does this print?
fn main() {
let mut v = vec![10, 20, 30];
for x in &mut v {
*x += 1;
}
for (i, x) in v.iter().enumerate() {
println!("{i}: {x}");
}
let total: i32 = v.iter().sum();
println!("total {total}");
}Running it prints:
0: 11 1: 21 2: 31 total 63
Does this compile?
fn main() {
let v = vec![1, 2, 3];
let w = v;
println!("{w:?}");
println!("{v:?}");
}The compiler says:
error[E0382]: borrow of moved value: `v`
What does this print?
fn main() {
let v = vec![1, 2, 3];
let w = v.clone();
println!("{w:?}");
println!("{v:?}");
}Running it prints:
[1, 2, 3] [1, 2, 3]
What does this print?
fn main() {
let v = vec![1, 2, 3];
let mut w = v.clone();
w.push(4);
println!("{v:?}");
println!("{w:?}");
}Running it prints:
[1, 2, 3] [1, 2, 3, 4]
What does this print?
fn takes(v: Vec<i32>) {
println!("{v:?}");
}
fn main() {
let v = vec![1, 2, 3];
takes(v);
println!("done");
}Running it prints:
[1, 2, 3] done
Does this compile?
fn takes(v: Vec<i32>) {
println!("{v:?}");
}
fn main() {
let v = vec![1, 2, 3];
takes(v);
println!("{v:?}");
}The compiler says:
error[E0382]: borrow of moved value: `v`
Does this compile?
fn main() {
let mut v = vec![1, 2, 3];
let w = v;
v = vec![9, 9];
println!("{v:?}");
println!("{w:?}");
}Running it prints:
[9, 9] [1, 2, 3]
What does this print?
fn total(items: &[i32]) -> i32 {
let mut sum = 0;
for item in items {
sum += item;
}
sum
}
fn main() {
let a = [1, 2, 3];
let v = vec![10, 20, 30, 40];
println!("{}", total(&a));
println!("{}", total(&v));
}Running it prints:
6 100
What does this print?
fn main() {
let v = vec![10, 20, 30, 40, 50];
let part = &v[1..3];
println!("{part:?}");
}Running it prints:
[20, 30]
What does this print?
fn first(items: &[i32]) -> i32 {
items[0]
}
fn main() {
let v = vec![7, 8, 9];
println!("{}", first(&v[1..]));
}Running it prints:
8
What does this print?
fn main() {
let v = vec![1, 2, 3, 4, 5];
let s = &v[..3];
println!("{} {:?}", s.len(), s);
}Running it prints:
3 [1, 2, 3]
Does this compile?
fn total(items: &Vec<i32>) -> i32 {
let mut sum = 0;
for item in items {
sum += item;
}
sum
}
fn main() {
let a = [1, 2, 3];
println!("{}", total(&a));
}The compiler says:
error[E0308]: mismatched types
What does this print?
fn main() {
let v = vec![1, 2, 3];
let s = &v[..];
println!("{s:?}");
}Running it prints:
[1, 2, 3]
What does this print?
fn make_end(n: usize) -> usize {
n + 10
}
fn main() {
let v = vec![1, 2, 3];
let end = make_end(0);
let s = &v[0..end];
println!("{s:?}");
}Running it prints nothing.
Then it panics: range end index 10 out of range for slice of length 3
Does this compile?
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
v.push(4);
println!("{first}");
}The compiler says:
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
What does this print?
fn main() {
let mut v = vec![1, 2, 3];
let first = &v[0];
println!("{first}");
v.push(4);
println!("{v:?}");
}Running it prints:
1 [1, 2, 3, 4]
Does this compile?
fn main() {
let mut v = vec![1, 2, 3];
let last = &v[v.len() - 1];
v.insert(0, 99);
println!("{last}");
}The compiler says:
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
Does this compile?
fn main() {
let mut v = vec![1, 2, 3];
let first = v[0];
v.push(4);
println!("{first}");
println!("{v:?}");
}Running it prints:
1 [1, 2, 3, 4]
What does this print?
fn main() {
let mut names: Vec<String> = Vec::new();
names.push(String::from("Ravi"));
names.push("Meera".to_string());
for name in &names {
println!("{name} has {} letters", name.len());
}
}Running it prints:
Ravi has 4 letters Meera has 5 letters
What does this print?
fn main() {
let line = "learning rust is fun";
let words: Vec<&str> = line.split_whitespace().collect();
println!("{words:?}");
println!("{}", words.len());
}Running it prints:
["learning", "rust", "is", "fun"] 4
What does this print?
fn main() {
let line = "one two three";
let words: Vec<&str> = line.split_whitespace().collect();
println!("{}", words[0]);
println!("{}", words[words.len() - 1]);
}Running it prints:
one three
What does this print?
fn pick(n: usize) -> usize {
n + 5
}
fn main() {
let line = "red green";
let words: Vec<&str> = line.split_whitespace().collect();
let i = pick(0);
println!("{}", words[i]);
}Running it prints nothing.
Then it panics: index out of bounds: the len is 2 but the index is 5
What does this print?
fn main() {
let names = vec![
String::from("Ann"),
String::from("Bartholomew"),
String::from("Cy"),
];
let mut total = 0;
let mut longest = &names[0];
for name in &names {
total += name.len();
if name.len() > longest.len() {
longest = name;
}
}
println!("total letters {total}");
println!("longest {longest}");
}Running it prints:
total letters 16 longest Bartholomew