1
1
# Statements
2
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/functions.html ) .
3
+ A * statement * is a component of a [ block] , which is in turn a component of an
4
+ outer [ expression] or [ function] .
5
5
6
6
Rust has two kinds of statement: [ declaration
7
7
statements] ( #declaration-statements ) and [ expression
8
8
statements] ( #expression-statements ) .
9
9
10
10
## Declaration statements
11
11
12
- A _ declaration statement _ is one that introduces one or more * names* into the
12
+ A * declaration statement * is one that introduces one or more * names* into the
13
13
enclosing statement block. The declared names may denote new variables or new
14
- items.
14
+ [ items] [ item ] .
15
+
16
+ The two kinds of declaration statements are item declarations and ` let `
17
+ statements.
15
18
16
19
### Item declarations
17
20
18
- An _ item declaration statement_ has a syntactic form identical to an
19
- [ item] ( items.html ) declaration within a module. Declaring an item &mdash ; a
20
- function, enumeration, struct, type, static, trait, implementation or module
21
- &mdash ; 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.
21
+ An * item declaration statement* has a syntactic form identical to an
22
+ [ item declaration] [ item ] within a [ module] . Declaring an item within a statement
23
+ block restricts its scope to the block containing the statement. The item is not
24
+ given a [ canonical path] nor are any sub-items it may declare. The exception to
25
+ this is that associated items defined by [ implementations] are still accessible
26
+ in outer scopes as long as the item and, if applicable, trait are accessible.
27
+ It is otherwise identical in meaning to declaring the item inside a module.
28
+
29
+ There is no implicit capture of the containing function's generic parameters,
30
+ parameters, and local variables. For example, ` inner ` may not access
31
+ ` outer_var ` .
32
+
33
+ ``` rust
34
+ fn outer () {
35
+ let outer_var = true ;
24
36
25
- > ** Note** : there is no implicit capture of the function's dynamic environment when
26
- > declaring a function-local item.
37
+ fn inner () { /* outer_var is not in scope here */ }
38
+
39
+ inner ();
40
+ }
41
+ ```
27
42
28
43
### ` let ` statements
29
44
30
- A _ ` let ` statement _ introduces a new set of variables, given by a pattern. The
45
+ A * ` let ` statement * introduces a new set of variables, given by a pattern. The
31
46
pattern may be followed by a type annotation, and/or an initializer expression.
32
47
When no type annotation is given, the compiler will infer the type, or signal
33
48
an error if insufficient type information is available for definite inference.
@@ -36,13 +51,11 @@ declaration until the end of the enclosing block scope.
36
51
37
52
## Expression statements
38
53
39
- An _ expression statement_ is one that evaluates an
40
- [ expression] ( expressions.html ) and ignores its result. As a rule, an expression
41
- statement's purpose is to trigger the effects of evaluating its expression.
42
- An expression that consists of only a [ block
43
- expression] ( expressions/block-expr.html ) or control flow expression,
44
- that doesn't end a block and evaluates to ` () ` can also be used as an
45
- expression statement by omitting the trailing semicolon.
54
+ An * expression statement* is one that evaluates an [ expression] and ignores its
55
+ result. As a rule, an expression statement's purpose is to trigger the effects
56
+ of evaluating its expression. An expression that consists of only a [ block
57
+ expression] [ block ] or control flow expression and that does not end a block
58
+ can also be used as an expression statement by omitting the trailing semicolon.
46
59
47
60
``` rust
48
61
# let mut v = vec! [1 , 2 , 3 ];
@@ -54,3 +67,11 @@ if v.is_empty() {
54
67
} // Semicolon can be omitted.
55
68
[1 ]; // Separate expression statement, not an indexing expression.
56
69
```
70
+
71
+ [ block ] : expressions/block-expr.html
72
+ [ expression ] : expressions.html
73
+ [ function ] : items/functions.html
74
+ [ item ] : items.html
75
+ [ module ] : items/modules.html
76
+ [ canonical path ] : path.html#canonical-paths
77
+ [ implementations ] : items/implementations.html
0 commit comments