Question 2Easy
Why does let c = s[0]; not compile when s is a String?
- Strings are immutable so they cannot be indexed
- Indexing a String always needs an unsafe block
- Strings are stored in reverse byte order internally
- 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
- Bytes vs chars is ambiguous.
- Rust refuses to guess.
- 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]?
- A slice [10, 20, 30]
- A slice [20, 30, 40]
- A new owned Vec [20, 30]
- 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
- Start index 1.
- End index 3 is excluded.
- 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]);
- ell
- hel
- ello
- 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
- The range 1..4 covers indices 1, 2 and 3, not 4.
- Index 0 is h, so index 1 is e.
- 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 6Medium
What is printed?
fn main() {
println!("{} {}", "a,b,,c".split(',').count(), "a,b,,c".split(',').filter(|x| !x.is_empty()).count());
}
- 3 3
- 4 3
- 3 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
- Splitting on commas gives a, b, an empty string and c.
- count() on that split is 4.
- 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(" "));
}
- pear apple fig
- fig apple pear
- apple fig pear
- 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
- BTreeMap stores its keys sorted, not in insertion order.
- The three keys are apple, fig and pear.
- 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 9Medium
What is printed?
fn main() {
let mut v = vec![1, 1, 2, 2, 1, 3];
v.dedup();
println!("{:?}", v);
}
- [1, 2, 3]
- [1, 1, 2, 2, 1, 3]
- [1, 2]
- [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
- dedup() compares each element with the one before it.
- 1, 1 becomes 1; 2, 2 becomes 2; the later 1 is not next to another 1, so it stays.
- 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, 4, 5, 6] [2, 3]
- [1, 2, 3, 4, 5, 6] [2, 3]
- [1, 2, 3, 6] [4, 5]
- [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
- The range 1..3 covers indices 1 and 2, the values 2 and 3.
- drain removes those values from v and yields them.
- 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.