Reverse a String
Level: easy (score: 2)
💡 Individual exercises are great, but nothing beats building a real-world app with Rust.
Check out our Rust Intro Cohort Program!
Implement the reverse_string
function that takes a string slice (&str
) as input and returns a new String
with the characters in reverse order.
Given the input string "hello"
, the function should return "olleh"
:
let reversed = reverse_string("hello"); // "olleh"
fn reverse_string(input: &str) -> String {
// Reverse and return the input string
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reverse_string() {
assert_eq!(reverse_string("hello"), "olleh");
assert_eq!(reverse_string("rust"), "tsur");
}
}