keyword

Rust else: provide an alternative branch

Use `else` to handle the alternative when an if condition is not met. An if/else expression can also choose a value to assign to a binding. Expected output: Sold out Why: The false condition selects the else expression. Common mistake: When the expression supplies a value, the branch result types must be compatible.

Formal Syntax & Structure

rust syntaxrust
if condition { expression } else { expression }

Working Example

Example Snippetrust
fn main() {    let stock = 0;    let label = if stock > 0 { "Available" } else { "Sold out" };    println!("{label}");}
Rust Reference