Going through rustlings for the first time
at main 1.2 kB view raw
1fn trim_me(input: &str) -> &str { 2 input.trim() 3} 4 5fn compose_me(input: &str) -> String { 6 // The macro `format!` has the same syntax as `println!`, but it returns a 7 // string instead of printing it to the terminal. 8 // Equivalent to `input.to_string() + " world!"` 9 format!("{input} world!") 10} 11 12fn replace_me(input: &str) -> String { 13 input.replace("cars", "balloons") 14} 15 16fn main() { 17 // You can optionally experiment here. 18} 19 20#[cfg(test)] 21mod tests { 22 use super::*; 23 24 #[test] 25 fn trim_a_string() { 26 assert_eq!(trim_me("Hello! "), "Hello!"); 27 assert_eq!(trim_me(" What's up!"), "What's up!"); 28 assert_eq!(trim_me(" Hola! "), "Hola!"); 29 } 30 31 #[test] 32 fn compose_a_string() { 33 assert_eq!(compose_me("Hello"), "Hello world!"); 34 assert_eq!(compose_me("Goodbye"), "Goodbye world!"); 35 } 36 37 #[test] 38 fn replace_a_string() { 39 assert_eq!( 40 replace_me("I think cars are cool"), 41 "I think balloons are cool", 42 ); 43 assert_eq!( 44 replace_me("I love to look at cars"), 45 "I love to look at balloons", 46 ); 47 } 48}