expression

Rust Block expressions: calculate a value

A block groups work into a scope and can produce a result. Use one to calculate a value with temporary local names. Expected output (not executed): 24 Why: The multiplication is the final expression and supplies the result. Common mistake: Adding a semicolon after the multiplication makes this normally completing block produce `()` instead of a number.

Formal Syntax & Structure

rust syntaxrust
{ statements; tail_expression }

Working Example

Example Snippetrust
fn main() {    let total = {        let price = 8;        let quantity = 3;        price * quantity    };    println!("{total}");}
Rust Reference