rustlings/exercises/03_if/if2.rs

38 lines
926 B
Rust
Raw Normal View History

2024-05-22 09:16:50 -04:00
// TODO: Fix the compiler error on this function.
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
2020-05-01 00:17:17 -04:00
} else {
1
}
}
fn main() {
// You can optionally experiment here.
}
2024-05-22 09:16:50 -04:00
// TODO: Read the tests to understand the desired behavior.
// Make all tests pass without changing them.
2020-05-01 00:17:17 -04:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn yummy_food() {
// This means that calling `picky_eater` with the argument "food" should return "Yummy!".
assert_eq!(picky_eater("strawberry"), "Yummy!");
2020-05-01 00:17:17 -04:00
}
#[test]
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
2020-05-01 00:17:17 -04: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!");
2020-05-01 00:17:17 -04:00
}
}