rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

if3.rs (1045B)


      1 fn animal_habitat(animal: &str) -> &str {
      2     // TODO: Fix the compiler error in the statement below.
      3     let identifier = if animal == "crab" {
      4         1
      5     } else if animal == "gopher" {
      6         2
      7     } else if animal == "snake" {
      8         3
      9     } else {
     10         0
     11     };
     12 
     13     // Don't change the expression below!
     14     if identifier == 1 {
     15         "Beach"
     16     } else if identifier == 2 {
     17         "Burrow"
     18     } else if identifier == 3 {
     19         "Desert"
     20     } else {
     21         "Unknown"
     22     }
     23 }
     24 
     25 fn main() {
     26     // You can optionally experiment here.
     27 }
     28 
     29 // Don't change the tests!
     30 #[cfg(test)]
     31 mod tests {
     32     use super::*;
     33 
     34     #[test]
     35     fn gopher_lives_in_burrow() {
     36         assert_eq!(animal_habitat("gopher"), "Burrow")
     37     }
     38 
     39     #[test]
     40     fn snake_lives_in_desert() {
     41         assert_eq!(animal_habitat("snake"), "Desert")
     42     }
     43 
     44     #[test]
     45     fn crab_lives_on_beach() {
     46         assert_eq!(animal_habitat("crab"), "Beach")
     47     }
     48 
     49     #[test]
     50     fn unknown_animal() {
     51         assert_eq!(animal_habitat("dinosaur"), "Unknown")
     52     }
     53 }