Challenge - 5 Problems
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code snippet?
Consider the following Rust code that manipulates a String. What will be printed?
DSA Rust
fn main() {
let mut s = String::from("hello");
s.push_str(", world");
println!("{}", s);
}Attempts:
2 left
💡 Hint
Look carefully at the push_str argument and how it adds to the original string.
✗ Incorrect
The push_str method appends the exact string ", world" to the original string "hello", resulting in "hello, world".
🧠 Conceptual
intermediateHow is a Rust String stored in memory?
Which of the following best describes the memory representation of a Rust String?
Attempts:
2 left
💡 Hint
Think about how Rust manages growable strings efficiently.
✗ Incorrect
Rust's String type stores a pointer to a heap buffer, along with length and capacity fields on the stack, allowing dynamic resizing.
🔧 Debug
advancedWhat error does this Rust code produce?
Analyze the following Rust code. What error will it cause when compiled?
DSA Rust
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
}Attempts:
2 left
💡 Hint
Remember Rust's ownership rules and what happens when you assign one String to another.
✗ Incorrect
Assigning s1 to s2 moves ownership, so s1 is no longer valid. Using s1 after move causes a compile-time error.
❓ Predict Output
advancedWhat is the output of this Rust code involving slicing?
What will this Rust program print?
DSA Rust
fn main() {
let s = String::from("hello world");
let slice = &s[0..5];
println!("{}", slice);
}Attempts:
2 left
💡 Hint
Look at the slice range and what part of the string it extracts.
✗ Incorrect
The slice &s[0..5] extracts characters from index 0 up to but not including 5, which is "hello".
🧠 Conceptual
expertWhy does Rust String slicing require valid UTF-8 boundaries?
Why must Rust string slices (&str) always be on valid UTF-8 character boundaries?
Attempts:
2 left
💡 Hint
Think about how UTF-8 encodes characters with varying byte lengths.
✗ Incorrect
UTF-8 characters can be multiple bytes. Slicing must not split a character byte sequence, or it would create invalid UTF-8 data.
