Skip to content

Commit 9f5694b

Browse files
committed
Add "scopes" chapter.
1 parent e1abb17 commit 9f5694b

File tree

8 files changed

+343
-14
lines changed

8 files changed

+343
-14
lines changed

src/expressions/block-expr.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
A *block expression*, or *block*, is a control flow expression and anonymous namespace scope for items and variable declarations.
1616
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
1717
As an anonymous namespace scope, item declarations are only in scope inside the block itself and variables declared by `let` statements are in scope from the next statement until the end of the block.
18+
See the [scopes] chapter for more details.
1819

1920
The syntax for a block is `{`, then any [inner attributes], then any number of [statements], then an optional expression, called the final operand, and finally a `}`.
2021

@@ -181,6 +182,7 @@ fn is_unix_platform() -> bool {
181182
[inner attributes]: ../attributes.md
182183
[method]: ../items/associated-items.md#methods
183184
[mutable reference]: ../types/pointer.md#mutables-references-
185+
[scopes]: ../names/scopes.md
184186
[shared references]: ../types/pointer.md#shared-references-
185187
[statement]: ../statements.md
186188
[statements]: ../statements.md

src/items.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,10 @@ There are several kinds of items:
5353
* [implementations]
5454
* [`extern` blocks]
5555

56-
Some items form an implicit scope for the declaration of sub-items. In other
57-
words, within a function or module, declarations of items can (in many cases)
58-
be mixed with the statements, control blocks, and similar artifacts that
59-
otherwise compose the item body. The meaning of these scoped items is the same
60-
as if the item was declared outside the scope — it is still a static item
61-
— except that the item's *path name* within the module namespace is
62-
qualified by the name of the enclosing item, or is private to the enclosing
63-
item (in the case of functions). The grammar specifies the exact locations in
64-
which sub-item declarations may appear.
56+
Items may be declared in the [root of the crate], a [module][modules], or a [statement].
57+
Additionally, a subset of items, called [associated items], may be declared in [traits] and [implementations].
58+
59+
See [item scopes] for information on the scoping rules of items.
6560

6661
[_ConstantItem_]: items/constant-items.md
6762
[_Enumeration_]: items/enumerations.md
@@ -83,14 +78,19 @@ which sub-item declarations may appear.
8378
[`extern crate` declarations]: items/extern-crates.md
8479
[`extern` blocks]: items/external-blocks.md
8580
[`use` declarations]: items/use-declarations.md
81+
[associated items]: items/associated-items.md
8682
[constant items]: items/constant-items.md
8783
[enumeration definitions]: items/enumerations.md
8884
[function definitions]: items/functions.md
8985
[implementations]: items/implementations.md
86+
[item scopes]: names/scopes.md#item-scopes
9087
[modules]: items/modules.md
9188
[paths]: paths.md
89+
[root of the crate]: crates-and-source-files.md
90+
[statement]: statements.md
9291
[static items]: items/static-items.md
9392
[struct definitions]: items/structs.md
9493
[trait definitions]: items/traits.md
94+
[traits]: items/traits.md
9595
[type definitions]: items/type-aliases.md
9696
[union definitions]: items/unions.md

src/items/generics.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ parameters are listed in angle <span class="parenthetical">brackets (`<...>`)</s
2323
usually immediately after the name of the item and before its definition. For
2424
implementations, which don't have a name, they come directly after `impl`.
2525
The order of generic parameters is restricted to lifetime parameters, then type parameters, and then const parameters.
26+
The same parameter name may not be declared more than once in a _GenericParams_ list.
2627

2728
Some examples of items with type, const, and lifetime parameters:
2829

@@ -36,6 +37,7 @@ struct InnerArray<T, const N: usize>([T; N]);
3637
Generic parameters are in scope within the item definition where they are
3738
declared. They are not in scope for items declared within the body of a
3839
function as described in [item declarations].
40+
See [generic parameter scopes] for more details.
3941

4042
[References], [raw pointers], [arrays], [slices][arrays], [tuples], and
4143
[function pointers] have lifetime or type parameters as well, but are not
@@ -296,6 +298,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
296298
[functions]: functions.md
297299
[function pointers]: ../types/function-pointer.md
298300
[generic implementations]: implementations.md#generic-implementations
301+
[generic parameter scopes]: ../names/scopes.md#generic-parameter-scopes
299302
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
300303
[implementations]: implementations.md
301304
[item declarations]: ../statements.md#item-declarations

src/names.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Some entities are [explicitly declared](#explicitly-declared-entities) in the
1313
source code, and some are [implicitly declared](#implicitly-declared-entities)
1414
as part of the language or compiler extensions.
1515

16-
[*Paths*] are used to refer to an entity, possibly in another scope. Lifetimes
16+
[*Paths*] are used to refer to an entity, possibly in another module or type. Lifetimes
1717
and loop labels use a [dedicated syntax][lifetimes-and-loop-labels] using a
1818
leading quote.
1919

0 commit comments

Comments
 (0)