Skip to content

Commit 36fc52c

Browse files
Merge pull request #177 from Havvy/fix-25
Better describe the scope of items defined in item declaration statements
2 parents 8b668fc + d708308 commit 36fc52c

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

Diff for: src/paths.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Paths
22

3-
A _path_ is a sequence of one or more path components _logically_ separated by
4-
a namespace qualifier (`::`). If a path consists of only one component, it may
5-
refer to either an [item] or a [variable] in a local control
6-
scope. If a path has multiple components, it refers to an item.
3+
A *path* is a sequence of one or more path components _logically_ separated by
4+
a namespace qualifier (`::`). If a path consists of only one component, it
5+
refers to either an [item] or a [variable] in a local control
6+
scope. If a path has multiple components, it always refers to an item.
77

88
Two examples of simple paths consisting of only identifier components:
99

Diff for: src/statements.md

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
# Statements
22

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].
55

66
Rust has two kinds of statement: [declaration
77
statements](#declaration-statements) and [expression
88
statements](#expression-statements).
99

1010
## Declaration statements
1111

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
1313
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.
1518

1619
### Item declarations
1720

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.
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;
2436

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+
```
2742

2843
### `let` statements
2944

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
3146
pattern may be followed by a type annotation, and/or an initializer expression.
3247
When no type annotation is given, the compiler will infer the type, or signal
3348
an error if insufficient type information is available for definite inference.
@@ -36,13 +51,11 @@ declaration until the end of the enclosing block scope.
3651

3752
## Expression statements
3853

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.
4659

4760
```rust
4861
# let mut v = vec![1, 2, 3];
@@ -54,3 +67,11 @@ if v.is_empty() {
5467
} // Semicolon can be omitted.
5568
[1]; // Separate expression statement, not an indexing expression.
5669
```
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

Comments
 (0)