Overview - Expression vs statement
What is it?
In Rust, an expression is a piece of code that produces a value, like a math calculation or a function call. A statement is a piece of code that performs an action but does not return a value, like creating a variable or calling a function without using its result. Expressions can be part of statements, but statements cannot be part of expressions. Understanding the difference helps you write clear and correct Rust code.
Why it matters
Knowing the difference between expressions and statements helps you understand how Rust runs your code and how to write code that works as expected. Without this, you might get errors or write code that doesn't do what you want. For example, Rust requires blocks to end with an expression if you want to return a value, so mixing these up can cause bugs or confusion.
Where it fits
Before learning this, you should know basic Rust syntax like variables, functions, and blocks. After this, you can learn about control flow, ownership, and how Rust evaluates code to produce results.
