Rust Strings Quiz
40 comprehensive questions on Rust's string types, covering String vs &str, UTF-8 behavior, slicing, mutating strings, and conversions — with 11 code examples demonstrating practical string handling patterns.
Question 1
What are the two main string types in Rust?
Question 2
What is the difference between String and &str?
Question 3
How do you create an empty String?
Question 4
How do you create a String from a string literal?
Question 5
What will this code do?
fn main() {
let s = "hello";
s.push_str(" world");
println!("{}", s);
}Question 6
How do you append to a String?
Question 7
What does UTF-8 mean for Rust strings?
Question 8
Why can't you index strings with integers?
Question 9
How do you safely access individual characters?
Question 10
What will this code output?
fn main() {
let s = "Здравствуйте";
println!("Length in bytes: {}", s.len());
println!("Length in chars: {}", s.chars().count());
}Question 11
How do you safely slice strings?
Question 12
What happens if you slice at invalid UTF-8 boundaries?
Question 13
How do you convert String to &str?
Question 14
How do you convert &str to String?
Question 15
What is string concatenation using +?
Question 16
What is the format! macro used for?
Question 17
How do you check if a string contains a substring?
Question 18
How do you replace text in a string?
Question 19
What does trim() do to a string?
Question 20
How do you split a string?
Question 21
What is the difference between split() and split_whitespace()?
Question 22
How do you convert numbers to strings?
Question 23
How do you parse strings to numbers?
Question 24
What does parse() return?
Question 25
How do you handle parse errors?
Question 26
What is string interning?
Question 27
How do you compare strings in Rust?
Question 28
What is the capacity of a String?
Question 29
How do you reserve capacity in a String?
Question 30
What happens when you push to a String at capacity?
Question 31
How do you clear a String?
Question 32
What is the difference between clear() and truncate(0)?
Question 33
How do you get a substring from a specific position?
Question 34
What does chars().nth(5) return?
Question 35
How do you reverse a string?
Question 36
What is string escaping?
Question 37
What are raw strings in Rust?
Question 38
How do you write a multiline string?
Question 39
What is the most efficient way to build a string from many parts?
Question 40
When should you prefer &str over String?
