rustlings/solutions/03_if/if2.rs

36 lines
750 B
Rust
Raw Normal View History

fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else if food == "potato" {
"I guess I can eat that."
2024-05-22 14:16:50 +01:00
} else {
"No thanks!"
2024-05-22 14:16:50 +01:00
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn yummy_food() {
assert_eq!(picky_eater("strawberry"), "Yummy!");
2024-05-22 14:16:50 +01:00
}
#[test]
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
2024-05-22 14:16:50 +01:00
}
#[test]
fn default_disliked_food() {
assert_eq!(picky_eater("broccoli"), "No thanks!");
assert_eq!(picky_eater("gummy bears"), "No thanks!");
assert_eq!(picky_eater("literally anything"), "No thanks!");
2024-05-22 14:16:50 +01:00
}
}