keyword

Rust if: run code conditionally

Use `if` to choose whether a block of code runs. For an ordinary Boolean condition, Rust requires a value of type bool rather than treating nonzero numbers as true. Expected output: Hot Why: The comparison evaluates to true, so the block executes. Common mistake: A number alone is not a Boolean condition.

Formal Syntax & Structure

rust syntaxrust
if condition { expression }

Working Example

Example Snippetrust
fn main() {    let temperature = 31;    if temperature > 30 {        println!("Hot");    }}
Rust Reference