Strings & Collections Questions with Answers

10 practice questions on Strings & Collections (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Easy

What does this print?

let mut v = vec![1, 2, 3];
v.pop();
v.push(9);
println!("{:?}", v);
  1. [1, 2, 9]
  2. [1, 2, 3, 9]
  3. [2, 3, 9]
  4. [9]

Answer: A. [1, 2, 9]

Explanation

pop removes the last element (3), then push(9) appends 9.

Step-by-step solution

  1. Start [1,2,3].
  2. pop gives [1,2].
  3. push gives [1,2,9].

Why the other options are wrong

  • B. pop removed 3 first.
  • C. pop removes the last element, not the first.
  • D. Other elements remain.

Question 2Easy

Why does let c = s[0]; not compile when s is a String?

  1. Strings are immutable so they cannot be indexed
  2. Indexing a String always needs an unsafe block
  3. Strings are stored in reverse byte order internally
  4. UTF-8 characters vary in byte width

Answer: D. UTF-8 characters vary in byte width

Explanation

Rust forbids s[i] because a byte index may not fall on a character boundary. Use slices like &s[0..1] or chars().nth(0).

Step-by-step solution

  1. Bytes vs chars is ambiguous.
  2. Rust refuses to guess.
  3. Use chars() or slices.

Why the other options are wrong

  • A. Mutability is not the reason.
  • B. It is simply not implemented.
  • C. Storage order is normal.

Question 3Easy

What does &v[1..3] produce for v = vec![10, 20, 30, 40]?

  1. A slice [10, 20, 30]
  2. A slice [20, 30, 40]
  3. A new owned Vec [20, 30]
  4. A slice [20, 30]

Answer: D. A slice [20, 30]

Explanation

Range 1..3 covers indexes 1 and 2 (end exclusive), borrowed as a slice.

Step-by-step solution

  1. Start index 1.
  2. End index 3 is excluded.
  3. Elements 20 and 30.

Why the other options are wrong

  • A. The end index is exclusive and the start is 1.
  • B. The slice stops before index 3.
  • C. The & makes it a borrowed slice, not a Vec.

Question 4Easy

What is printed?

let s = String::from("hello");
println!("{}", &s[1..4]);
  1. ell
  2. hel
  3. ello
  4. hell

Answer: A. ell

Explanation

String ranges use byte positions, and the range 1..4 covers positions 1, 2 and 3. Those hold e, l and l, so the output is ell.

Step-by-step solution

  1. The range 1..4 covers indices 1, 2 and 3, not 4.
  2. Index 0 is h, so index 1 is e.
  3. Indices 1 to 3 are e, l, l, which prints ell.

Why the other options are wrong

  • B. That is 0..3; the range starts at index 1.
  • C. That is 1..5, which includes the o at index 4; the range 1..4 stops before it.
  • D. That is 0..4; the range starts at index 1.

Tip: Ranges are start-inclusive, end-exclusive.

Question 5Medium

What is printed?

use std::collections::HashMap;
let mut m = HashMap::new();
for w in ["a", "b", "a"] {
    *m.entry(w).or_insert(0) += 1;
}
println!("{}", m["a"]);
  1. 1
  2. 2
  3. 3
  4. 0

Answer: B. 2

Explanation

entry(w).or_insert(0) yields a mutable counter that is incremented per occurrence; "a" occurs twice.

Step-by-step solution

  1. First a inserts 0 then adds 1.
  2. Second a adds 1.
  3. Count is 2.

Why the other options are wrong

  • A. a appears twice.
  • C. Only two a's exist.
  • D. The counter is incremented, not left at 0.

Question 6Medium

What is printed?

fn main() {
    println!("{} {}", "a,b,,c".split(',').count(), "a,b,,c".split(',').filter(|x| !x.is_empty()).count());
}
  1. 3 3
  2. 4 3
  3. 3 4
  4. 4 4

Answer: B. 4 3

Explanation

split(',') keeps the empty piece between the two commas, so "a,b,,c" gives four pieces: a, b, empty and c. Filtering out the empty piece leaves three.

Step-by-step solution

  1. Splitting on commas gives a, b, an empty string and c.
  2. count() on that split is 4.
  3. Filtering out empty strings leaves a, b, c, so the second count is 3.

Why the other options are wrong

  • A. split still yields the empty piece, so the first count is 4.
  • C. The first count includes the empty piece, so it is 4; the filtered count is 3.
  • D. filter removes the empty piece, so the second count is 3.

Tip: split keeps empty pieces; filter them out if you need to.

Question 7Medium

What is printed?

use std::collections::BTreeMap;

fn main() {
    let mut m = BTreeMap::new();
    m.insert("pear", 1);
    m.insert("apple", 2);
    m.insert("fig", 3);
    let keys: Vec<&str> = m.keys().copied().collect();
    println!("{}", keys.join(" "));
}
  1. pear apple fig
  2. fig apple pear
  3. apple fig pear
  4. pear fig apple

Answer: C. apple fig pear

Explanation

A BTreeMap keeps its keys in sorted order, whatever the insertion order. Sorted, the keys are apple, fig and pear.

Step-by-step solution

  1. BTreeMap stores its keys sorted, not in insertion order.
  2. The three keys are apple, fig and pear.
  3. In ascending order the join prints apple fig pear.

Why the other options are wrong

  • A. That is insertion order; a BTreeMap sorts its keys.
  • B. Not sorted: fig is placed before apple, but a BTreeMap lists apple first.
  • D. That is descending order; BTreeMap iterates in ascending order.

Tip: BTreeMap = sorted keys; HashMap = no order.

Question 8Medium

What does this print?

let s = "héllo";
println!("{}", s.len());
  1. 5
  2. 7
  3. 6
  4. 4

Answer: C. 6

Explanation

len returns the byte length in UTF-8. The accented é uses two bytes, so 4 ASCII letters plus 2 gives 6; use chars().count() for 5.

Step-by-step solution

  1. len counts bytes.
  2. é is 2 bytes in UTF-8.
  3. 1+2+1+1+1 = 6.

Why the other options are wrong

  • A. len counts bytes, not characters.
  • B. é takes two bytes, not three.
  • D. All five characters count.

Question 9Medium

What is printed?

fn main() {
    let mut v = vec![1, 1, 2, 2, 1, 3];
    v.dedup();
    println!("{:?}", v);
}
  1. [1, 2, 3]
  2. [1, 1, 2, 2, 1, 3]
  3. [1, 2]
  4. [1, 2, 1, 3]

Answer: D. [1, 2, 1, 3]

Explanation

dedup removes only consecutive duplicates. The two 1s at the start become one, the two 2s become one, and the later 1 stays because its neighbours are 2 and 3.

Step-by-step solution

  1. dedup() compares each element with the one before it.
  2. 1, 1 becomes 1; 2, 2 becomes 2; the later 1 is not next to another 1, so it stays.
  3. The result is [1, 2, 1, 3].

Why the other options are wrong

  • A. dedup removes only neighbouring duplicates, so the later 1 stays.
  • B. dedup does remove the adjacent duplicates, so the list changes.
  • C. The 3 at the end is not a duplicate, so it stays.

Tip: dedup only removes neighbours; sort first to remove all duplicates.

Question 10Hard

What is printed?

fn main() {
    let mut v = vec![1, 2, 3, 4, 5, 6];
    let d: Vec<i32> = v.drain(1..3).collect();
    println!("{:?} {:?}", v, d);
}
  1. [1, 4, 5, 6] [2, 3]
  2. [1, 2, 3, 4, 5, 6] [2, 3]
  3. [1, 2, 3, 6] [4, 5]
  4. [2, 3] [1, 4, 5, 6]

Answer: A. [1, 4, 5, 6] [2, 3]

Explanation

drain(1..3) removes the elements at indices 1 and 2, which are 2 and 3, and yields them. collect() puts them in d, and v keeps the rest: 1, 4, 5 and 6.

Step-by-step solution

  1. The range 1..3 covers indices 1 and 2, the values 2 and 3.
  2. drain removes those values from v and yields them.
  3. collect() puts 2 and 3 into d, leaving v as [1, 4, 5, 6].

Why the other options are wrong

  • B. drain removes the elements from v; it does not copy them.
  • C. The end of 1..3 is excluded, so index 3 (value 4) is not drained.
  • D. The drained values go to d and v keeps the rest; the two are swapped.

Tip: drain removes a range from the Vec and yields it.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics