strings3.rs (1203B)
1 fn trim_me(input: &str) -> &str { 2 input.trim() 3 } 4 5 fn 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 12 fn replace_me(input: &str) -> String { 13 input.replace("cars", "balloons") 14 } 15 16 fn main() { 17 // You can optionally experiment here. 18 } 19 20 #[cfg(test)] 21 mod 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 assert_eq!(trim_me("Hi!"), "Hi!"); 30 } 31 32 #[test] 33 fn compose_a_string() { 34 assert_eq!(compose_me("Hello"), "Hello world!"); 35 assert_eq!(compose_me("Goodbye"), "Goodbye world!"); 36 } 37 38 #[test] 39 fn replace_a_string() { 40 assert_eq!( 41 replace_me("I think cars are cool"), 42 "I think balloons are cool", 43 ); 44 assert_eq!( 45 replace_me("I love to look at cars"), 46 "I love to look at balloons", 47 ); 48 } 49 }