Rust's expression-based syntax allows code blocks to evaluate to values, enabling concise assignments and control flow without explicit return statements.
Rust is expression-based, meaning almost every block of code evaluates to a value, allowing you to omit semicolons to return results directly. This contrasts with statement-based languages where blocks return nothing unless explicitly returned.
fn main() {
let x = if true { 5 } else { 6 }; // Returns 5
let y = { let z = 10; z + 1 }; // Returns 11
}
In Rust, code blocks act like math equations that result in a value rather than just performing an action. This means you can assign the result of a complex block of logic directly to a variable without needing a separate return statement. Think of it like a calculator where every step produces a number you can immediately use in the next step.