Skip to content

Commit 6cc34fa

Browse files
authored
Merge pull request #1588 from jrcarl624/main
added if3 based on: `Using if in a let Statement`
2 parents c5b0627 + 2871726 commit 6cc34fa

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

exercises/if/if3.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// if3.rs
2+
//
3+
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
4+
5+
// I AM NOT DONE
6+
7+
pub fn animal_habitat(animal: &str) -> &'static str {
8+
let identifier = if animal == "crab" {
9+
1
10+
} else if animal == "gopher" {
11+
2.0
12+
} else if animal == "snake" {
13+
3
14+
} else {
15+
"Unknown"
16+
};
17+
18+
// DO NOT CHANGE THIS STATEMENT BELOW
19+
let habitat = if identifier == 1 {
20+
"Beach"
21+
} else if identifier == 2 {
22+
"Burrow"
23+
} else if identifier == 3 {
24+
"Desert"
25+
} else {
26+
"Unknown"
27+
};
28+
29+
habitat
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn gopher_lives_in_burrow() {
38+
assert_eq!(animal_habitat("gopher"), "Burrow")
39+
}
40+
41+
#[test]
42+
fn snake_lives_in_desert() {
43+
assert_eq!(animal_habitat("snake"), "Desert")
44+
}
45+
46+
#[test]
47+
fn crab_lives_on_beach() {
48+
assert_eq!(animal_habitat("crab"), "Beach")
49+
}
50+
51+
#[test]
52+
fn unknown_animal() {
53+
assert_eq!(animal_habitat("dinosaur"), "Unknown")
54+
}
55+
}

info.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ For that first compiler error, it's important in Rust that each conditional
167167
block returns the same type! To get the tests passing, you will need a couple
168168
conditions checking different input values."""
169169

170+
[[exercises]]
171+
name = "if3"
172+
path = "exercises/if/if3.rs"
173+
mode = "test"
174+
hint = """
175+
In Rust, every arm of an `if` expression has to return the same type of value. Make sure the type is consistent across all arms."""
176+
170177
# QUIZ 1
171178

172179
[[exercises]]

0 commit comments

Comments
 (0)