]> git.lizzy.rs Git - rust.git/blob - src/doc/reference/src/statements.md
statements and expressions
[rust.git] / src / doc / reference / src / statements.md
1 # Statements
2
3 A _statement_ is a component of a block, which is in turn a component of an
4 outer [expression](expressions.html) or [function](items.html#functions).
5
6 Rust has two kinds of statement: [declaration
7 statements](#declaration-statements) and [expression
8 statements](#expression-statements).
9
10 ## Declaration statements
11
12 A _declaration statement_ is one that introduces one or more *names* into the
13 enclosing statement block. The declared names may denote new variables or new
14 items.
15
16 ### Item declarations
17
18 An _item declaration statement_ has a syntactic form identical to an
19 [item](items.html) declaration within a module. Declaring an item — a
20 function, enumeration, struct, type, static, trait, implementation or module
21 — locally within a statement block is simply a way of restricting its
22 scope to a narrow region containing all of its uses; it is otherwise identical
23 in meaning to declaring the item outside the statement block.
24
25 > **Note**: there is no implicit capture of the function's dynamic environment when
26 > declaring a function-local item.
27
28 ### `let` statements
29
30 A _`let` statement_ introduces a new set of variables, given by a pattern. The
31 pattern may be followed by a type annotation, and/or an initializer expression.
32 When no type annotation is given, the compiler will infer the type, or signal
33 an error if insufficient type information is available for definite inference.
34 Any variables introduced by a variable declaration are visible from the point of
35 declaration until the end of the enclosing block scope.
36
37 ## Expression statements
38
39 An _expression statement_ is one that evaluates an [expression](expressions.html)
40 and ignores its result. The type of an expression statement `e;` is always
41 `()`, regardless of the type of `e`. As a rule, an expression statement's
42 purpose is to trigger the effects of evaluating its expression.