You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/names/scopes.md
+39-14
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
A *scope* is the region of source text where a named [entity] may be referenced with that name.
4
4
The following sections provide details on the scoping rules and behavior, which depend on the kind of entity and where it is declared.
5
5
The process of how names are resolved to entities is described in the [name resolution] chapter.
6
-
More information on "drop scopes" used for the purpose of running destructors maybe be found in the [destructors] chapter.
6
+
More information on "drop scopes" used for the purpose of running destructors may be found in the [destructors] chapter.
7
7
8
8
## Item scopes
9
9
@@ -20,10 +20,10 @@ A [path] may be used to refer to an item in another module.
20
20
21
21
### Associated item scopes
22
22
23
-
[Associated items] are not scoped and can only be referred to by using a [path] leading from the type they are associated with.
23
+
[Associated items] are not scoped and can only be referred to by using a [path] leading from the type or trait they are associated with.
24
24
[Methods] can also be referred to via [call expressions].
25
25
26
-
Similar to items within a module or block, it is an error to introduce an item within a trait or implementation that is a duplicate of another item in the trait or impl in the same namespace.
26
+
Similar to items within a module or block, it is an error to introduce an item within a trait or implementation that is a duplicate of another item in the trait or impl in the same namespace.
27
27
28
28
## Pattern binding scopes
29
29
@@ -90,24 +90,33 @@ fn where_scope<'a, T, U>()
90
90
{}
91
91
```
92
92
93
-
Generic scopes do not extend into [items] declared inside a function.
93
+
It is an error for [items] declared inside a function to refer to a generic parameter from their outer scope.
94
+
95
+
```rust,compile_fail
96
+
fn example<T>() {
97
+
fn inner(x: T) {} // ERROR: can't use generic parameters from outer function
98
+
}
99
+
```
94
100
95
101
### Generic parameter shadowing
96
102
97
-
It is an error to shadow a generic parameter.
98
-
Items declared within functions are allowed to reuse generic parameter names from the function because generic scopes do not extend to inner items.
103
+
It is an error to shadow a generic parameter with the exception that items declared within functions are allowed to shadow generic parameter names from the function.
99
104
100
105
```rust
101
-
fnexample<'a>() {
102
-
// Items within functions are allowed to reuse generic parameter in scope
103
-
// because all generics are not in scope within inner items.
104
-
fninner<'a>() {} // OK
106
+
fnexample<'a, T, constN:usize>() {
107
+
// Items within functions are allowed to shadow generic parameter in scope.
108
+
fninner_lifetime<'a>() {} // OK
109
+
fninner_type<T>() {} // OK
110
+
fninner_const<constN:usize>() {} // OK
105
111
}
106
112
```
107
113
108
114
```rust,compile_fail
109
-
trait SomeTrait<'a> {
110
-
fn example<'a>() {} // ERROR: 'a is already in scope
115
+
trait SomeTrait<'a, T, const N: usize> {
116
+
fn example_lifetime<'a>() {} // ERROR: 'a is already in use
117
+
fn example_type<T>() {} // ERROR: T is already in use
118
+
fn example_const<const N: usize>() {} // ERROR: N is already in use
119
+
fn example_mixed<const T: usize>() {} // ERROR: T is already in use
111
120
}
112
121
```
113
122
@@ -262,11 +271,27 @@ Helper attributes shadow other attributes of the same name in scope.
262
271
Although [`Self`] is a keyword with special meaning, it interacts with name resolution in a way similar to normal names.
263
272
264
273
The implicit `Self` type in the definition of a [struct], [enum], [union], [trait], or [implementation] is treated similarly to a [generic parameter](#generic-parameter-scopes), and is in scope in the same way as a generic type parameter.
265
-
For structs, enums, and unions, it is in scope starting after the generic parameters.
266
-
For traits and implementations, it is in scope starting just before the generic parameters.
267
274
268
275
The implicit `Self` constructor in the value [namespace] of an [implementation] is in scope within the body of the implementation (the implementation's [associated items]).
269
276
277
+
```rust
278
+
// Self type within struct definition.
279
+
structRecursive {
280
+
f1:Option<Box<Self>>
281
+
}
282
+
283
+
// Self type within generic parameters.
284
+
structSelfGeneric<T:Into<Self>>(T);
285
+
286
+
// Self value constructor within an implementation.
0 commit comments