Conditional statement is based on {if, else} keywords. You can create two program branches. When a condition is true then first branch is executed. When the condition is false the other branch is executed. The else branch is optional but the first is mandatory.
Conditional Execution
fn main() {
let a = 10;
let b = 10;
if a == b {
println!("a and b are equal"); //expected
}
else
{
println!("a and b are not equal"); //unexpected
}
}
In many languages the decision is only a statement. In Rust the if is actually an expression that can have a result. The result is given by the value of the last expression in the block. This is specific to Rust and looks a little bit strange for programmer. Sometimes may be useful to think like this especially when there is no ternary operator like "?" used in other languages.
/* demo for conditional expression */
fn main() {
let condition: bool = false; //boolean
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number);
}
Note:
5
and 6
are enclosed in a small blocks { 5 }, {6}bool,
6
except if you modify it.In Python we use elsif keyword and in Julia we use elseif keyword to create a multi-deck decision. Here in Rust we do not have a such keywords. Instead you can use else if that are two keywords. This is possible in Rust due to lack of any symbol required after the "else". In Python after else you must use ":" but in Rust you can use a block { ... } or another if statement to for a multi-path decision block.
Decision Ladder
fn main() {
use std::io;
/* read a number */
println!("enter a number:");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let number: i32 = input.trim().parse().unwrap();
/* check number */
if number % 2 == 0 {
println!("this number is divisible by 2");
} else if number % 3 == 0 {
println!("this number is divisible by 3");
} else if number % 5 == 0 {
println!("this number is divisible by 5");
} else if number % 7 == 0 {
println!("this number is divisible by 7");
} else {
println!("this number is not divisible by 2,3,5 or 7");
}
println!("done");
}
Homework:Open this example and run it: ladder
There is one more control statement that is very important in Rust. This will replace "switch" or "case" that is available in Julia and Level. The idea of this statement is simple. We create a structure to check several conditions. We execute one statement for witch the condition is true. The beauty of Rust is that this condition is verified such way that all cases are covered. If we do not cover all cases a compiler error will be generated. "match" is one of the most powerful things in Rust.
/* define enumeration */
enum Coin {
Nickel,
Dime,
Quarter,
}
/* use "match" statement */
fn penny(coin: Coin) -> u32 {
match coin {
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
/* test penny() function */
fn main() {
use Coin::{Quarter, Dime, Nickel};
println!("A quarter has {} penny",penny(Quarter));
println!("A dime has {} penny",penny(Dime));
println!("A Nickel has {} penny",penny(Nickel));
}
Homework: This example is on repl.it. Open and run it: penny
Read next: Repetition