rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

strings3.rs (1225B)


      1 fn trim_me(input: &str) -> &str {
      2     // TODO: Remove whitespace from both ends of a string.
      3     input.trim()
      4 }
      5 
      6 fn compose_me(input: &str) -> String {
      7     // TODO: Add " world!" to the string! There are multiple ways to do this.
      8     format!("{input} world!")
      9 }
     10 
     11 fn replace_me(input: &str) -> String {
     12     // TODO: Replace "cars" in the string with "balloons".
     13     input.replace("cars", "balloons").to_string()
     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 }