What Is the Difference Between a Statement and an Expression in Rust?

Expressions in Rust return values and can be used anywhere, whereas statements perform actions and do not return values.

In Rust, an expression evaluates to a value, while a statement performs an action and returns no value. You can use expressions anywhere a value is expected, but statements are limited to specific contexts like the body of a function.

fn main() {
    // Expression: evaluates to 5, assigned to x
    let x = 5;

    // Statement: performs assignment, returns ()
    let y = 10;

    // Expression: evaluates to 15, assigned to z
    let z = x + y;

    // Statement: prints value, returns ()
    println!("z is {z}");
}