From eafb157d60ee46e95e2b54ec75b33180a838b0c5 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 22 May 2024 15:16:50 +0200 Subject: [PATCH] if2 solution --- exercises/03_if/if2.rs | 8 ++++---- rustlings-macros/info.toml | 6 ++++-- solutions/03_if/if2.rs | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/exercises/03_if/if2.rs b/exercises/03_if/if2.rs index 1b65596b..593a77a7 100644 --- a/exercises/03_if/if2.rs +++ b/exercises/03_if/if2.rs @@ -1,6 +1,4 @@ -// Step 1: Make me compile! -// Step 2: Get the bar_for_fuzz and default_to_baz tests passing! - +// TODO: Fix the compiler error on this function. fn foo_if_fizz(fizzish: &str) -> &str { if fizzish == "fizz" { "foo" @@ -13,13 +11,15 @@ fn main() { // You can optionally experiment here. } -// No test changes needed! +// TODO: Read the tests to understand the desired behavior. +// Make all tests pass without changing them. #[cfg(test)] 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"); } diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index a67e38d6..39fc5c4d 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -209,8 +209,10 @@ name = "if2" dir = "03_if" hint = """ For that first compiler error, it's important in Rust that each conditional -block returns the same type! To get the tests passing, you will need a couple -conditions checking different input values.""" +block returns the same type! + +To get the tests passing, you will need a couple conditions checking different +input values. Read the tests to find out what they expect.""" [[exercises]] name = "if3" diff --git a/solutions/03_if/if2.rs b/solutions/03_if/if2.rs index 4e181989..440bba05 100644 --- a/solutions/03_if/if2.rs +++ b/solutions/03_if/if2.rs @@ -1 +1,33 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +fn foo_if_fizz(fizzish: &str) -> &str { + if fizzish == "fizz" { + "foo" + } else if fizzish == "fuzz" { + "bar" + } else { + "baz" + } +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn foo_for_fizz() { + assert_eq!(foo_if_fizz("fizz"), "foo"); + } + + #[test] + fn bar_for_fuzz() { + assert_eq!(foo_if_fizz("fuzz"), "bar"); + } + + #[test] + fn default_to_baz() { + assert_eq!(foo_if_fizz("literally anything"), "baz"); + } +}