rustlings/exercises/03_if/if2.rs

43 lines
949 B
Rust
Raw Normal View History

2020-05-01 07:17:17 +03:00
// if2.rs
//
2020-05-01 07:17:17 +03:00
// Step 1: Make me compile!
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
//
2022-07-12 12:10:08 +03:00
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
2020-05-01 07:17:17 +03:00
pub fn foo_if_fizz(fizzish: &str) -> &str {
2024-01-31 19:09:42 +03:00
// &str is a string slice -
// Let's say the full string is a ribbon
// A string slice is a piece of the ribbon
// It's just a reference to the ribbon so doing things to the piece won't change the original ribbon.
2020-05-01 07:17:17 +03:00
if fizzish == "fizz" {
"foo"
2024-01-31 19:09:42 +03:00
} else if fizzish == "fuzz" {
"bar"
2020-05-01 07:17:17 +03:00
} else {
2024-01-31 19:09:42 +03:00
"baz"
2020-05-01 07:17:17 +03:00
}
}
// No test changes needed!
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn foo_for_fizz() {
assert_eq!(foo_if_fizz("fizz"), "foo")
2020-05-01 07:17:17 +03:00
}
#[test]
fn bar_for_fuzz() {
assert_eq!(foo_if_fizz("fuzz"), "bar")
2020-05-01 07:17:17 +03:00
}
#[test]
fn default_to_baz() {
assert_eq!(foo_if_fizz("literally anything"), "baz")
2020-05-01 07:17:17 +03:00
}
}