Search⌘ K

Functions Returning Booleans

Explore how to write Rust functions that return booleans based on conditions like temperature checks. Understand using comparison operators and assertions to control program logic. Practice creating functions that evaluate if temperatures are hot or cold, and learn to refine assertions for clearer code behavior.

We'll cover the following...

Since bool is a type just like i32, we can return it from functions.

Rust 1.40.0
fn temperature() -> i32 {
15 // feel free to change this!
}
fn is_hot() -> bool {
temperature() > 30
}
fn main() {
assert!(is_hot() == false);
println!("Success!");
}

Here, we have a temperature function that pretends to get the ...