File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -167,6 +167,13 @@ For that first compiler error, it's important in Rust that each conditional
167
167
block returns the same type! To get the tests passing, you will need a couple
168
168
conditions checking different input values."""
169
169
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
+
170
177
# QUIZ 1
171
178
172
179
[[exercises ]]
You can’t perform that action at this time.
0 commit comments