Skip to content

Commit

Permalink
Merge pull request rust-lang#2110 from senekor/remo/skkynvtqxkoz
Browse files Browse the repository at this point in the history
Make if2 less confusing
  • Loading branch information
mo8it committed Sep 16, 2024
2 parents 1bae2dc + b540c6d commit 2894f3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
22 changes: 12 additions & 10 deletions exercises/03_if/if2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: Fix the compiler error on this function.
fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else {
1
}
Expand All @@ -18,18 +18,20 @@ mod tests {
use super::*;

#[test]
fn foo_for_fizz() {
// This means that calling `foo_if_fizz` with the argument "fizz" should return "foo".
assert_eq!(foo_if_fizz("fizz"), "foo");
fn yummy_food() {
// This means that calling `picky_eater` with the argument "food" should return "Yummy!".
assert_eq!(picky_eater("strawberry"), "Yummy!");
}

#[test]
fn bar_for_fuzz() {
assert_eq!(foo_if_fizz("fuzz"), "bar");
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
}

#[test]
fn default_to_baz() {
assert_eq!(foo_if_fizz("literally anything"), "baz");
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!");
}
}
26 changes: 14 additions & 12 deletions solutions/03_if/if2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else if food == "potato" {
"I guess I can eat that."
} else {
"baz"
"No thanks!"
}
}

Expand All @@ -17,17 +17,19 @@ mod tests {
use super::*;

#[test]
fn foo_for_fizz() {
assert_eq!(foo_if_fizz("fizz"), "foo");
fn yummy_food() {
assert_eq!(picky_eater("strawberry"), "Yummy!");
}

#[test]
fn bar_for_fuzz() {
assert_eq!(foo_if_fizz("fuzz"), "bar");
fn neutral_food() {
assert_eq!(picky_eater("potato"), "I guess I can eat that.");
}

#[test]
fn default_to_baz() {
assert_eq!(foo_if_fizz("literally anything"), "baz");
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!");
}
}

0 comments on commit 2894f3c

Please sign in to comment.