Rust If/Else Statement
- Authors
- Name
- Brent
If/Else in Rust
If-Else staments in Rust are similar to other languages.
In Rust, the boolean condition doesn't need to be surrounded by parentheses, and each condition is followed by a block.
If-Else conditionals are expressions, all branches must return the same type.
Example
let n = 10;
if n < 0 {
print!("{} is negative", n);
} else if n > 0 {
print!("{} is positive", n);
} else {
print!("{} is zero", n);
}
Output
10 is positvie