Expressions and Statements
The last expression.
Expressions and Statements is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}Expressions Evaluate to Values
Arithmetic, function calls, and literals are all expressions because they produce a value.
52 + 3square(4)
fn main() {
let v = 2 + 3 * 4;
println!("{}", v);
}Blocks Are Expressions
A block { ... } is itself an expression. Its value is the last expression inside, if it has no trailing semicolon.
fn main() {
let y = {
let x = 3;
x + 1
};
println!("y is {}", y);
}The Semicolon Matters
Adding a semicolon turns an expression into a statement, discarding its value.
x + 1— expression, yields a value.x + 1;— statement, yields().
fn main() {
let y = {
let x = 3;
x + 1 // no semicolon: this is the block's value
};
println!("{}", y);
}The Last Expression Returns
In a function, the final expression with no semicolon becomes the return value. This is idiomatic Rust.
fn main() {
println!("{}", five());
}
fn five() -> i32 {
5
}Semicolon Breaks Returns
If you accidentally add a semicolon to the last line, the function returns () instead of your value — a common beginner error.
fn main() {
println!("{}", plus_one(10));
}
fn plus_one(n: i32) -> i32 {
n + 1 // correct: no semicolon
}if as an Expression
Because if is an expression, you can assign its result to a variable. Both branches must yield the same type.
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("number is {}", number);
}loop as an Expression
A loop can return a value through break. The value after break becomes the loop's result.
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("result {}", result);
}match as an Expression
A match is also an expression. Each arm produces a value, and the matched arm's value is the result.
fn main() {
let n = 2;
let label = match n {
1 => "one",
2 => "two",
_ => "many",
};
println!("{}", label);
}Putting It Together
Treating blocks and control flow as expressions makes Rust concise: you compute and return in one fluid step.
fn main() {
let score = 85;
let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' };
println!("grade {}", grade);
}Quick Check
What does adding a semicolon do to an expression?
Recap
Expressions and statements in Rust:
- Statements (like
let) do work but return no value. - Expressions evaluate to a value.
- Blocks,
if,match, andloopare expressions. - The last expression without a semicolon is the return value.
fn main() {
let x = { let a = 2; a * a };
let y = if x > 3 { "big" } else { "small" };
println!("{} {}", x, y);
}Frequently asked questions
Is the “Expressions and Statements” lesson free?
Yes — the full text of “Expressions and Statements” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Expressions and Statements”?
The last expression. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Expressions and Statements” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Functions
- Expressions and Statements
- Closures
- Fn, FnMut, FnOnce