Bird
Raised Fist0
DSA Rustprogramming~10 mins

String Reversal Approaches in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to reverse a string using the built-in method.

DSA Rust
let original = String::from("hello");
let reversed: String = original.chars().rev().[1]();
println!("{}", reversed);
Drag options to blanks, or click blank then click option'
Ato_string()
Bclone()
Cunwrap()
Dcollect()
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_string() instead of collect() after rev()
Trying to unwrap() the iterator
Using clone() which does not reverse
2fill in blank
medium

Complete the code to reverse a string by converting it to a vector of chars and reversing in place.

DSA Rust
let mut chars: Vec<char> = original.chars().collect();
chars.[1]();
let reversed: String = chars.into_iter().collect();
println!("{}", reversed);
Drag options to blanks, or click blank then click option'
Arev
Breverse
Csort
Dsort_unstable
Attempts:
3 left
💡 Hint
Common Mistakes
Using rev() on Vec which is invalid
Using sort() which sorts instead of reversing
Using sort_unstable() which also sorts
3fill in blank
hard

Fix the error in the code to reverse a string by swapping characters in place.

DSA Rust
let mut chars: Vec<char> = original.chars().collect();
let len = chars.len();
for i in 0..len / 2 {
    chars.swap(i, len - 1 - [1]);
}
let reversed: String = chars.into_iter().collect();
println!("{}", reversed);
Drag options to blanks, or click blank then click option'
Ai
B0
Clen
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using len or 0 instead of i causes index errors
Using 1 shifts the index incorrectly
4fill in blank
hard

Fill in the blank to create a reversed string using iterator methods and filter vowels.

DSA Rust
let reversed_vowels: String = original.chars().rev().filter(|c| "aeiouAEIOU".[1](c)).collect();
Drag options to blanks, or click blank then click option'
Ais_ascii
Bstarts_with
Ccontains
Dends_with
Attempts:
3 left
💡 Hint
Common Mistakes
Using starts_with or ends_with which check prefixes or suffixes
Using is_ascii which checks character encoding
5fill in blank
hard

Fill all three blanks to create a reversed string with uppercase letters only.

DSA Rust
let reversed_uppercase: String = original.chars().rev().filter(|c| c.is_alphabetic() && c.[1]()).map(|c| c.[2]()).flatten().collect::<String>().[3]();
Drag options to blanks, or click blank then click option'
Ais_lowercase
Bto_uppercase
Cto_string
Dto_lowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_lowercase instead of to_uppercase
Not converting the iterator to String properly
Filtering with is_uppercase instead of is_lowercase

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

  1. Step 1: Understand chars() method

    The chars() method converts the string into an iterator over its characters.
  2. Step 2: Apply rev() and collect()

    The rev() method reverses the iterator, and collect() gathers the reversed characters into a new string.
  3. Final Answer:

    It reverses the characters of the string and collects them into a new string. -> Option A
  4. Quick Check:

    chars().rev().collect() reverses string [OK]
Hint: Remember: chars() + rev() + collect() reverses a string [OK]
Common Mistakes:
  • Thinking it sorts characters instead of reversing
  • Confusing rev() with removing characters
  • Assuming it changes case of letters
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

  1. Step 1: Check method availability

    Rust strings do not have a reverse() method, so let reversed: String = s.reverse(); is invalid.
  2. 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.
  3. Final Answer:

    let reversed: String = s.chars().rev().collect(); -> Option B
  4. 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

  1. Step 1: Understand input string

    The string "rust" has characters 'r', 'u', 's', 't'.
  2. Step 2: Reverse characters and collect

    Using chars().rev().collect() reverses the characters to 't', 's', 'u', 'r' and collects into a new string.
  3. Final Answer:

    tsur -> Option C
  4. 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

  1. Step 1: Check method existence on String

    Rust's String type does not have a reverse() method, so calling it causes a compile error.
  2. Step 2: Confirm other syntax correctness

    The semicolon after println! is present, and printing a string is valid, so no errors there.
  3. Final Answer:

    String type has no method named reverse() -> Option D
  4. 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

  1. Step 1: Understand Unicode grapheme clusters

    Some characters like emojis or accented letters are multiple Unicode code points but one visible character (grapheme cluster).
  2. 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.
  3. Final Answer:

    Use the unicode-segmentation crate to split by graphemes, then reverse and collect. -> Option A
  4. Quick Check:

    unicode-segmentation crate handles grapheme reversal correctly [OK]
Hint: Use unicode-segmentation crate for correct Unicode string reversal [OK]
Common Mistakes:
  • Reversing by chars() breaks multi-codepoint characters
  • Reversing bytes corrupts UTF-8 encoding
  • Reversing words instead of characters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard