-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif-else.rs
More file actions
36 lines (32 loc) · 1.06 KB
/
if-else.rs
File metadata and controls
36 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Example of an if-else statement in Rust
fn main() {
// If/else statements are similar to other languages.
if true {
println!("true");
} else if false {
println!("false");
} else {
println!("neither");
}
// Rust supports six comparison operators: '==', '!=', '<',' >', '<=', '>='.
// and two boolean operators: '&&' (and) and '||' (or).
let n = 42;
if n > 0 && n < 100 {
println!("n is between 0 and 100");
} else if n < 0 || n > 100 {
println!("n is not between 0 and 100");
}
// The 'if' statement can return a value and can be used in a 'let'
// statement.
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number);
// Using 'if let' expression, control is determined through pattern
// matching instead of a conditional expression.
let result: Option<i32> = Some(42);
if let Some(value) = result {
println!("The value is: {}", value);
} else {
println!("There is no value");
}
}