Portal/Notes πŸ“
RustRustlings_exercise

If

Versi Bahasa Indonesia. Baca dalam Bahasa Inggris

  • if adalah sebuah expression.
  • Semua expression if dimulai dengan keyword if, diikuti dengan sebuah kondisi.
  • Perlu dicatat juga bahwa kondisi dalam kode ini harus berupa bool. Jika kondisinya bukan bool, kita akan mendapatkan error.
  • Karena if adalah sebuah expression, kita dapat menggunakannya di sisi kanan statement let untuk menetapkan hasilnya ke sebuah variabel.
  • Referensi: Control Flow

if1.rs

fn bigger(a: i32, b: i32) -> i32 {
    // fill the function body using if expression
    if a > b {
        a
    } else {
        b
    }
}

fn main() {
    // You can optionally experiment here.
}

// Don't mind this for now :)
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ten_is_bigger_than_eight() {
        assert_eq!(10, bigger(10, 8));
    }

    #[test]
    fn fortytwo_is_bigger_than_thirtytwo() {
        assert_eq!(42, bigger(32, 42));
    }

    #[test]
    fn equal_numbers() {
        assert_eq!(42, bigger(42, 42));
    }
}
  • Latihan ini sederhana, kita hanya perlu menambahkan expression if yang memeriksa variabel mana yang lebih besar di fungsi bigger.

    if a > b {
      a
    } else {
      b
    }
  • Karena if adalah sebuah expression, kita bisa langsung mengetik a dan b dan itu akan berfungsi sebagai nilai kembalian.

if2.rs

// TODO: Fix the compiler error on this function.
fn picky_eater(food: &str) -> &str {
    if food == "strawberry" {
        "Yummy!"
    } else if food == "potato" { // if potato return "I guess I can eat that."
        "I guess I can eat that."
    } else { // else return "No thanks!"
        "No thanks!"
    }
}

fn main() {
    // You can optionally experiment here.
}

// TODO: Read the tests to understand the desired behavior.
// Make all tests pass without changing them.
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn yummy_food() {
        // This means that calling `picky_eater` with the argument "food" should return "Yummy!".
        assert_eq!(picky_eater("strawberry"), "Yummy!");
    }

    #[test]
    fn neutral_food() {
        assert_eq!(picky_eater("potato"), "I guess I can eat that.");
    }

    #[test]
    fn default_disliked_food() {
        assert_eq!(picky_eater("broccoli"), "No thanks!");
        assert_eq!(picky_eater("gummy bears"), "No thanks!");
        assert_eq!(picky_eater("literally anything"), "No thanks!");
    }
}
  • Di contoh ini kita perlu melihat testcase yang sudah ditentukan sebelumnya.
    • Test1 - yummy_food: mengharapkan string "Yummy!" jika argumennya adalah "strawberry".
    • Test2 - neutral_food: mengharapkan string "I guess I can eat that." jika argumennya adalah "potato".
    • Test3 - default_disliked_food: mengharapkan string "broccoli", "gummy bears", atau "literally anything".
  • Kita kehilangan dua case di fungsi picky_eater.
  • Jadi mari tambahkan potato menggunakan else if dan sisanya masuk ke block else.

if3.rs

fn animal_habitat(animal: &str) -> &str {
    // Make sure all returned values have the same types
    let identifier = if animal == "crab" {
        1
    } else if animal == "gopher" {
        2 // Don't return float
    } else if animal == "snake" {
        3
    } else {
        0 // Use 0 as substitute of unknown values
    };

    // Don't change the expression below!
    if identifier == 1 {
        "Beach"
    } else if identifier == 2 {
        "Burrow"
    } else if identifier == 3 {
        "Desert"
    } else {
        "Unknown"
    }
}

fn main() {
    // You can optionally experiment here.
}

// Don't change the tests!
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gopher_lives_in_burrow() {
        assert_eq!(animal_habitat("gopher"), "Burrow")
    }

    #[test]
    fn snake_lives_in_desert() {
        assert_eq!(animal_habitat("snake"), "Desert")
    }

    #[test]
    fn crab_lives_on_beach() {
        assert_eq!(animal_habitat("crab"), "Beach")
    }

    #[test]
    fn unknown_animal() {
        assert_eq!(animal_habitat("dinosaur"), "Unknown")
    }
}
  • Karena if adalah sebuah expression, kita dapat menggunakannya di sisi kanan statement let untuk menetapkan hasilnya ke sebuah variabel.
  • Dalam kasus ini kita hanya perlu memastikan bahwa nilai yang dikembalikan dalam expression if dari identifier memiliki tipe yang sama.
  • Gunakan 0 sebagai pengganti nilai "unknown".

Edit on GitHub

Last updated on

If | Faisal Affan