Expressions and Statements
The last expression.
Statements vs Expressions
Rust distinguishes two kinds of code:
- Statements perform an action and return no value.
- Expressions evaluate to a value.
This distinction shapes how functions return values.
fn main() {
let x = 5; // a let statement
let y = x + 1; // x + 1 is an expression
println!("{}", y);
}let Is a Statement
A let binding is a statement. It does not return a value, so you cannot write let a = (let b = 6);.
fn main() {
let a = 6;
// let c = (let d = 6); // error: let is not an expression
println!("{}", a);
}All lessons in this course
- Defining Functions
- Expressions and Statements
- Closures
- Fn, FnMut, FnOnce