Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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);
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
✗ Incorrect
The rev() method returns an iterator, so we use collect() to gather the characters back into a String.
2fill in blank
mediumComplete 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);
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
✗ Incorrect
The reverse() method reverses the vector in place. rev() is an iterator adapter and cannot be called on Vec directly.
3fill in blank
hardFix 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);
Attempts:
3 left
💡 Hint
Common Mistakes
Using len or 0 instead of i causes index errors
Using 1 shifts the index incorrectly
✗ Incorrect
We swap the character at index i with the character at index len - 1 - i to reverse the vector in place.
4fill in blank
hardFill 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();
Attempts:
3 left
💡 Hint
Common Mistakes
Using starts_with or ends_with which check prefixes or suffixes
Using is_ascii which checks character encoding
✗ Incorrect
The contains() method checks if the vowels string contains the character c, filtering only vowels.
5fill in blank
hardFill 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]();
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
✗ Incorrect
We filter lowercase alphabetic characters, convert them to uppercase, collect into a String, then convert the iterator to String with to_string().
