Rust is an expression-oriented language, which means that most pieces of code are in fact expressions, that is, they compute a value and return that value (in that sense, values are also expressions). However, expressions by themselves do not form meaningful code; they must be used in statements.
The let
bindings like the following are declaration statements; they are not expressions:
// see Chapter 2/code/expressions.rs let a = 2; // a binds to 2 let b = 5; // b binds to 5 let n = a + b; // n binds to 7
However, a + b
is an expression, and if we omit the semicolon at the end, the resulting value (here 7
) is returned. This is often used when a function needs to return its value (see examples in the next chapter). Ending an expression with a semicolon like a + b;
suppresses the value of an expression, thereby throwing away the return value and making it an expression statement that returns the unit value ()
. A code is usually a sequence of statements, one on each code line...