if2.rs (750B)
1 fn picky_eater(food: &str) -> &str { 2 if food == "strawberry" { 3 "Yummy!" 4 } else if food == "potato" { 5 "I guess I can eat that." 6 } else { 7 "No thanks!" 8 } 9 } 10 11 fn main() { 12 // You can optionally experiment here. 13 } 14 15 #[cfg(test)] 16 mod tests { 17 use super::*; 18 19 #[test] 20 fn yummy_food() { 21 assert_eq!(picky_eater("strawberry"), "Yummy!"); 22 } 23 24 #[test] 25 fn neutral_food() { 26 assert_eq!(picky_eater("potato"), "I guess I can eat that."); 27 } 28 29 #[test] 30 fn default_disliked_food() { 31 assert_eq!(picky_eater("broccoli"), "No thanks!"); 32 assert_eq!(picky_eater("gummy bears"), "No thanks!"); 33 assert_eq!(picky_eater("literally anything"), "No thanks!"); 34 } 35 }