If..Else
Branch on a condition.
if / else
Run one block when the condition is true, otherwise the other.
fn main() {
let score = 75
if score >= 60 {
print("Pass")
} else {
print("Fail")
}
}fn main() -> void {
let score: i32 = 75
if score >= 60 {
print("Pass")
} else {
print("Fail")
}
}Output
Pass
if as expression
if can return a value directly.
fn main() {
let score = 95
let label = if score >= 90 { "A" } else { "B" }
print(label)
}fn main() -> void {
let score: i32 = 95
let label = if score >= 90 { "A" } else { "B" }
print(label)
}Output
A