Expressions

A Rust program is (mostly) made up of a series of statements:

1. fn main() {
2. // statement
3. // statement
4. // statement
5. }

There are a few kinds of statements in Rust. The most common two are declaring a variable binding, and using a ; with an expression:

1. fn main() {
2. // variable binding
3. let x = 5;

5. // expression;
6. x;
7. x + 1;
8. 15;
9. }

Blocks are expressions too, so they can be used as r-values in assignments. The last expression in the block will be assigned to the l-value. However, if the last expression of the block ends with a semicolon, the return value will be ().

1. fn main() {
2. let x = 5u32;

4. let y = {
5. let x_squared = x * x;
6. let x_cube = x_squared * x;

8. // This expression will be assigned to `y`
9. x_cube + x_squared + x
10. };

12. let z = {
13. // The semicolon suppresses this expression and `()` is assigned to `z`
14. 2 * x;
15. };

17. println!("x is {:?}", x);
18. println!("y is {:?}", y);
19. println!("z is {:?}", z);
20. }