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;
  4. // expression;
  5. x;
  6. x + 1;
  7. 15;
  8. }

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;
  3. let y = {
  4. let x_squared = x * x;
  5. let x_cube = x_squared * x;
  6. // This expression will be assigned to `y`
  7. x_cube + x_squared + x
  8. };
  9. let z = {
  10. // The semicolon suppresses this expression and `()` is assigned to `z`
  11. 2 * x;
  12. };
  13. println!("x is {:?}", x);
  14. println!("y is {:?}", y);
  15. println!("z is {:?}", z);
  16. }