]>
Commit | Line | Data |
---|---|---|
5869c6ff XL |
1 | # Names |
2 | ||
3 | An *entity* is a language construct that can be referred to in some way within | |
4 | the source program, usually via a [path]. Entities include [types], [items], | |
5 | [generic parameters], [variable bindings], [loop labels], [lifetimes], | |
6 | [fields], [attributes], and [lints]. | |
7 | ||
8 | A *declaration* is a syntactical construct that can introduce a *name* to | |
9 | refer to an entity. Entity names are valid within a [*scope*] — a region of | |
10 | source text where that name may be referenced. | |
11 | ||
12 | Some entities are [explicitly declared](#explicitly-declared-entities) in the | |
13 | source code, and some are [implicitly declared](#implicitly-declared-entities) | |
14 | as part of the language or compiler extensions. | |
15 | ||
16 | [*Paths*] are used to refer to an entity, possibly in another scope. Lifetimes | |
17 | and loop labels use a [dedicated syntax][lifetimes-and-loop-labels] using a | |
18 | leading quote. | |
19 | ||
20 | Names are segregated into different [*namespaces*], allowing entities in | |
21 | different namespaces to share the same name without conflict. | |
22 | ||
23 | [*Name resolution*] is the compile-time process of tying paths, identifiers, | |
24 | and labels to entity declarations. | |
25 | ||
26 | Access to certain names may be restricted based on their [*visibility*]. | |
27 | ||
28 | ## Explicitly declared entities | |
29 | ||
30 | Entities that explicitly introduce a name in the source code are: | |
31 | ||
32 | * [Items]: | |
33 | * [Module declarations] | |
34 | * [External crate declarations] | |
35 | * [Use declarations] | |
36 | * [Function declarations] and [function parameters] | |
37 | * [Type aliases] | |
38 | * [struct], [union], [enum], enum variant declarations, and their named | |
39 | fields | |
40 | * [Constant item declarations] | |
41 | * [Static item declarations] | |
42 | * [Trait item declarations] and their [associated items] | |
43 | * [External block items] | |
44 | * [`macro_rules` declarations] and [matcher metavariables] | |
45 | * [Implementation] associated items | |
46 | * [Expressions]: | |
47 | * [Closure] parameters | |
48 | * [`while let`] pattern bindings | |
49 | * [`for`] pattern bindings | |
50 | * [`if let`] pattern bindings | |
51 | * [`match`] pattern bindings | |
52 | * [Loop labels] | |
53 | * [Generic parameters] | |
54 | * [Higher ranked trait bounds] | |
55 | * [`let` statement] pattern bindings | |
56 | * The [`macro_use` attribute] can introduce macro names from another crate | |
57 | * The [`macro_export` attribute] can introduce an alias for the macro into the crate root | |
58 | ||
59 | Additionally, [macro invocations] and [attributes] can introduce names by | |
60 | expanding to one of the above items. | |
61 | ||
62 | ## Implicitly declared entities | |
63 | ||
64 | The following entities are implicitly defined by the language, or are | |
65 | introduced by compiler options and extensions: | |
66 | ||
67 | * [Language prelude]: | |
68 | * [Boolean type] — `bool` | |
69 | * [Textual types] — `char` and `str` | |
70 | * [Integer types] — `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128` | |
71 | * [Machine-dependent integer types] — `usize` and `isize` | |
72 | * [floating-point types] — `f32` and `f64` | |
73 | * [Built-in attributes] | |
74 | * [Standard library prelude] items, attributes, and macros | |
75 | * [Standard library][extern-prelude] crates in the root module | |
76 | * [External crates][extern-prelude] linked by the compiler | |
77 | * [Tool attributes] | |
78 | * [Lints] and [tool lint attributes] | |
79 | * [Derive helper attributes] are valid within an item without being explicitly imported | |
80 | * The [`'static`] lifetime | |
81 | ||
82 | Additionally, the crate root module does not have a name, but can be referred | |
83 | to with certain [path qualifiers] or aliases. | |
84 | ||
85 | ||
86 | [*Name resolution*]: names/name-resolution.md | |
87 | [*namespaces*]: names/namespaces.md | |
88 | [*paths*]: paths.md | |
89 | [*scope*]: names/scopes.md | |
90 | [*visibility*]: visibility-and-privacy.md | |
91 | [`'static`]: keywords.md#weak-keywords | |
92 | [`for`]: expressions/loop-expr.md#iterator-loops | |
93 | [`if let`]: expressions/if-expr.md#if-let-expressions | |
94 | [`let` statement]: statements.md#let-statements | |
95 | [`macro_export` attribute]: macros-by-example.md#path-based-scope | |
96 | [`macro_rules` declarations]: macros-by-example.md | |
97 | [`macro_use` attribute]: macros-by-example.md#the-macro_use-attribute | |
98 | [`match`]: expressions/match-expr.md | |
99 | [`while let`]: expressions/loop-expr.md#predicate-pattern-loops | |
100 | [associated items]: items/associated-items.md | |
101 | [attributes]: attributes.md | |
102 | [Boolean type]: types/boolean.md | |
103 | [Built-in attributes]: attributes.md#built-in-attributes-index | |
104 | [Closure]: expressions/closure-expr.md | |
105 | [Constant item declarations]: items/constant-items.md | |
106 | [Derive helper attributes]: procedural-macros.md#derive-macro-helper-attributes | |
107 | [enum]: items/enumerations.md | |
108 | [Expressions]: expressions.md | |
109 | [extern-prelude]: names/preludes.md#extern-prelude | |
110 | [External block items]: items/external-blocks.md | |
111 | [External crate declarations]: items/extern-crates.md | |
112 | [fields]: expressions/field-expr.md | |
113 | [floating-point types]: types/numeric.md#floating-point-types | |
114 | [Function declarations]: items/functions.md | |
115 | [function parameters]: items/functions.md#function-parameters | |
116 | [Generic parameters]: items/generics.md | |
117 | [Higher ranked trait bounds]: trait-bounds.md#higher-ranked-trait-bounds | |
118 | [Implementation]: items/implementations.md | |
119 | [Integer types]: types/numeric.md#integer-types | |
120 | [Items]: items.md | |
121 | [Language prelude]: names/preludes.md#language-prelude | |
122 | [lifetimes-and-loop-labels]: tokens.md#lifetimes-and-loop-labels | |
123 | [lifetimes]: tokens.md#lifetimes-and-loop-labels | |
124 | [Lints]: attributes/diagnostics.md#lint-check-attributes | |
125 | [Loop labels]: expressions/loop-expr.md#loop-labels | |
126 | [Machine-dependent integer types]: types/numeric.md#machine-dependent-integer-types | |
127 | [macro invocations]: macros.md#macro-invocation | |
128 | [matcher metavariables]: macros-by-example.md#metavariables | |
129 | [Module declarations]: items/modules.md | |
130 | [path]: paths.md | |
131 | [path qualifiers]: paths.md#path-qualifiers | |
132 | [Standard library prelude]: names/preludes.md#standard-library-prelude | |
133 | [Static item declarations]: items/static-items.md | |
134 | [struct]: items/structs.md | |
135 | [Textual types]: types/textual.md | |
136 | [Tool attributes]: attributes.md#tool-attributes | |
137 | [tool lint attributes]: attributes/diagnostics.md#tool-lint-attributes | |
138 | [Trait item declarations]: items/traits.md | |
139 | [Type aliases]: items/type-aliases.md | |
140 | [types]: types.md | |
141 | [union]: items/unions.md | |
142 | [Use declarations]: items/use-declarations.md | |
143 | [variable bindings]: patterns.md |