Rust Programming Language: Statements and Expressions

0
In Rust, function bodies consist of a series of statements optionally ending in an expression. Statements are instructions that perform actions but do not return values. On the other hand, expressions evaluate to a resultant value.

Rust
fn main() {
    let y = 6; // Statement
}
Here, let y = 6; is a statement, as it performs an action (creates a variable y and assigns it the value 6) but does not return a value.

Rust
fn main() {
    let y = {
        let x = 3;
        x + 1
    };

    println!("The value of y is: {y}");
}
Expressions do not end with semicolons, and they can be part of statements. For instance:
In this example, the block evaluates to 4, which is then bound to the variable y.

Post a Comment

0Comments
Post a Comment (0)