rustlings/exercises/03_if/if2.rs

38 lines
926 B
Rust
Raw Normal View History

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