Search⌘ K

Solution Review 1: Check If Even or Odd

Explore how to apply Rust if and else expressions to check whether a number is even or odd. Understand the use of the modulus operator and control flow to write simple conditionals effectively.

We'll cover the following...

Solution:

Rust 1.40.0
fn test(_a:i32) {
// check divisibility by 2
if _a % 2 == 0 { // execute if divisible
println!("Number {} is even", _a);
}
else { // execute if not divisible
println!("Number {} is odd", _a);
}
}

Explanation

Note: The statement a % b ...

svg viewer