2.15 Expressions vs. Statements
Rust is primarily an expression-based language. This means most constructs, including if blocks, match arms, and even simple code blocks {}, evaluate to a value.
- Expression: Something that evaluates to a value (e.g.,
5,x + 1,if condition { val1 } else { val2 },{ let a = 1; a + 2 }). - Statement: An action that performs some work but does not return a value. In Rust, statements are typically expressions ending with a semicolon
;. The semicolon discards the value of the expression, turning it into a statement. Variable declarations withletare also statements.
fn main() { // `let y = ...` is a statement. // The block `{ ... }` is an expression. let y = { let x = 3; x + 1 // No semicolon: this is the value the block evaluates to }; // Semicolon ends the `let` statement. println!("The value of y is: {}", y); // Prints 4 // Example of an if expression let condition = false; let z = if condition { 10 } else { 20 }; println!("The value of z is: {}", z); // Prints 20 // Example of a statement (discarding the block's value) { println!("This block doesn't return a value to assign."); }; // Semicolon is optional here as it's the last thing in `main`'s block }
2.15.1 Comparison with C
In C, the distinction between expressions and statements is stricter. For example, if/else constructs are statements, not expressions, and blocks {} do not inherently evaluate to a value that can be assigned directly. Assignments themselves (x = 5) are expressions in C, which allows constructs like if (x = y) that Rust prohibits in conditional contexts.