Search⌘ K

Else

Explore how to use else statements in Rust to manage conditional logic effectively. Learn to write clean, readable code for scenarios requiring alternative actions based on boolean conditions, and practice by creating a temperature-based function.

We'll cover the following...

I want a program that says “it’s hot” if it’s hot, and “it’s not hot” if it’s not hot. We can do this with two ifs with opposite conditions:

Rust 1.40.0
fn main() {
let is_hot = false;
if is_hot {
println!("It's hot!");
}
if !is_hot {
println!("It's not hot!");
}
}

But that ...