rustlings

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

if2.rs (1010B)


      1 // TODO: Fix the compiler error on this function.
      2 fn picky_eater(food: &str) -> &str {
      3     if food == "strawberry" {
      4         "Yummy!"
      5     } else if food == "potato" {
      6         "I guess I can eat that."
      7     } else {
      8         "No thanks!"
      9     }
     10 }
     11 
     12 fn main() {
     13     // You can optionally experiment here.
     14 }
     15 
     16 // TODO: Read the tests to understand the desired behavior.
     17 // Make all tests pass without changing them.
     18 #[cfg(test)]
     19 mod tests {
     20     use super::*;
     21 
     22     #[test]
     23     fn yummy_food() {
     24         // This means that calling `picky_eater` with the argument "strawberry" should return "Yummy!".
     25         assert_eq!(picky_eater("strawberry"), "Yummy!");
     26     }
     27 
     28     #[test]
     29     fn neutral_food() {
     30         assert_eq!(picky_eater("potato"), "I guess I can eat that.");
     31     }
     32 
     33     #[test]
     34     fn default_disliked_food() {
     35         assert_eq!(picky_eater("broccoli"), "No thanks!");
     36         assert_eq!(picky_eater("gummy bears"), "No thanks!");
     37         assert_eq!(picky_eater("literally anything"), "No thanks!");
     38     }
     39 }