What is the simplest way to reverse a string in Rust using built-in methods?
Use the <code>chars()</code> method to get an iterator over characters, then <code>rev()</code> to reverse it, and finally <code>collect()</code> to gather into a new string.<br>Example: <code>let reversed: String = original.chars().rev().collect();</code>
Click to reveal answer
beginner
Why can't we reverse a Rust string by simply reversing its bytes?
Because Rust strings are UTF-8 encoded, reversing bytes can break multi-byte characters, resulting in invalid strings. We must reverse by characters, not bytes, to keep the string valid.
Click to reveal answer
intermediate
How does reversing a string using a mutable vector of characters work in Rust?
Convert the string into a vector of characters using chars().collect(), then use reverse() method on the vector, and finally collect back into a string. This approach allows in-place reversal of characters.
Click to reveal answer
intermediate
What is a manual approach to reverse a string in Rust without using built-in reverse methods?
Manually iterate over the characters from the end to the start using a loop, appending each character to a new string. This shows how reversal logic works step-by-step.
Click to reveal answer
beginner
What is the time complexity of reversing a string in Rust using chars().rev().collect()?
The time complexity is O(n), where n is the number of characters in the string. Each character is visited once to reverse and collect into a new string.
Click to reveal answer
Which Rust method correctly reverses a string by characters?
Aoriginal.bytes().rev().collect()
Boriginal.chars().rev().collect()
Coriginal.reverse()
Doriginal.to_string().reverse()
✗ Incorrect
Only chars().rev().collect() reverses the string by characters safely. Reversing bytes can break UTF-8 encoding.
Why is reversing bytes of a Rust string unsafe?
ABecause UTF-8 characters can be multiple bytes
BBecause bytes are immutable
CBecause Rust strings are ASCII only
DBecause bytes are stored in reverse order
✗ Incorrect
UTF-8 characters can use multiple bytes, so reversing bytes can corrupt characters.
What does collect() do after reversing characters in Rust?
AConverts iterator into a vector
BRemoves duplicates
CConverts iterator into a string
DSorts the characters
✗ Incorrect
collect() gathers the reversed characters into a new string.
Which data structure can be used to reverse a string in-place in Rust?
AString slice &str
BHashMap<char, usize>
CTuple (char, char)
DVec<char>
✗ Incorrect
A vector of characters (Vec) can be reversed in-place using reverse().
What is the output of reversing the string "rust" using chars().rev().collect()?
A"tsur"
B"rust"
C"trus"
D"ruts"
✗ Incorrect
Reversing "rust" character-wise results in "tsur".
Explain how to reverse a UTF-8 string safely in Rust and why reversing bytes directly is problematic.
Think about how UTF-8 encoding affects string reversal.
You got /4 concepts.
Describe two different approaches to reverse a string in Rust and their key differences.
One uses iterator methods, the other uses a mutable vector.
You got /4 concepts.
Practice
(1/5)
1. What does the chars().rev().collect() method chain do when applied to a Rust string?
easy
A. It reverses the characters of the string and collects them into a new string.
B. It sorts the characters of the string in ascending order.
C. It removes all whitespace characters from the string.
D. It converts the string to uppercase.
Solution
Step 1: Understand chars() method
The chars() method converts the string into an iterator over its characters.
Step 2: Apply rev() and collect()
The rev() method reverses the iterator, and collect() gathers the reversed characters into a new string.
Final Answer:
It reverses the characters of the string and collects them into a new string. -> Option A
2. Which of the following Rust code snippets correctly reverses a string s?
easy
A. let reversed: String = s.reverse();
B. let reversed: String = s.chars().rev().collect();
C. let reversed: String = s.collect().rev().chars();
D. let reversed: String = s.chars().collect().rev();
Solution
Step 1: Check method availability
Rust strings do not have a reverse() method, so let reversed: String = s.reverse(); is invalid.
Step 2: Analyze method chaining
let reversed: String = s.chars().rev().collect(); uses chars() to get characters, rev() to reverse, and collect() to form a new string, which is correct.
Final Answer:
let reversed: String = s.chars().rev().collect(); -> Option B
Quick Check:
Correct syntax for string reversal is let reversed: String = s.chars().rev().collect(); [OK]
Hint: Use chars().rev().collect() to reverse strings in Rust [OK]
Common Mistakes:
Using non-existent reverse() method on String
Wrong order of method calls
Trying to reverse after collecting into String
3. What is the output of the following Rust code?
let s = String::from("rust");
let reversed: String = s.chars().rev().collect();
println!("{}", reversed);
medium
A. Error: cannot reverse string
B. rust
C. tsur
D. tusr
Solution
Step 1: Understand input string
The string "rust" has characters 'r', 'u', 's', 't'.
Step 2: Reverse characters and collect
Using chars().rev().collect() reverses the characters to 't', 's', 'u', 'r' and collects into a new string.
Final Answer:
tsur -> Option C
Quick Check:
Reversed "rust" = "tsur" [OK]
Hint: Trace chars().rev().collect() to get reversed string output [OK]
Common Mistakes:
Expecting original string output
Mixing character order incorrectly
Assuming runtime error on reversal
4. Identify the error in this Rust code that attempts to reverse a string:
let s = String::from("hello");
let reversed = s.reverse();
println!("{}", reversed);
medium
A. reverse() returns a vector, not a string
B. Missing semicolon after println! macro
C. Cannot print reversed string directly
D. String type has no method named reverse()
Solution
Step 1: Check method existence on String
Rust's String type does not have a reverse() method, so calling it causes a compile error.
Step 2: Confirm other syntax correctness
The semicolon after println! is present, and printing a string is valid, so no errors there.
Final Answer:
String type has no method named reverse() -> Option D
Quick Check:
reverse() method does not exist on String [OK]
Hint: Remember: String has no reverse() method in Rust [OK]
Common Mistakes:
Assuming reverse() exists on String
Thinking reverse() returns vector automatically
Ignoring compiler error messages
5. You want to reverse a string but keep all Unicode grapheme clusters intact (like emojis or accented letters). Which Rust crate and approach is best suited for this?
hard
A. Use the unicode-segmentation crate to split by graphemes, then reverse and collect.
B. Use chars().rev().collect() directly on the string.
C. Convert string to bytes, reverse bytes, then convert back to string.
D. Use split_whitespace() and reverse the words.
Solution
Step 1: Understand Unicode grapheme clusters
Some characters like emojis or accented letters are multiple Unicode code points but one visible character (grapheme cluster).
Step 2: Use appropriate crate and method
The unicode-segmentation crate provides graphemes() method to split string by grapheme clusters, which can then be reversed safely.
Final Answer:
Use the unicode-segmentation crate to split by graphemes, then reverse and collect. -> Option A