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/items/use-declarations.md
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,6 +203,21 @@ mod example {
203
203
> [!NOTE]
204
204
> `self` may also be used as the first segment of a path. The use of `self` as the first segment and inside a `use` brace is logically the same; it means the current module of the parent segment, or the current module if there is no parent segment. See [`self`] in the paths chapter for more information on the meaning of a leading `self`.
205
205
206
+
r[items.use.self.trailing]
207
+
`self` may appear as the last segment of a `use` path, preceded by `::`. A path of the form `P::self` is equivalent to `P::{self}`, and `P::self as name` is equivalent to `P::{self as name}`.
208
+
209
+
```rust
210
+
modm {
211
+
pubenumE { V1, V2 }
212
+
}
213
+
usem::selfas _; // Equivalent to `use m::{self as _};`.
214
+
usem::E::self; // Equivalent to `use m::E::{self};`.
215
+
# fnmain() {}
216
+
```
217
+
218
+
> [!NOTE]
219
+
> See [paths.qualifiers.mod-self.trailing] for restrictions on the preceding path.
220
+
206
221
r[items.use.self.module]
207
222
When `self` is used within [brace syntax], the path preceding the brace group must resolve to a [module], [enumeration], or [trait].
`self` resolves the path relative to the current module.
234
234
235
235
r[paths.qualifiers.mod-self.restriction]
236
-
`self` can only be used as the first segment, without a preceding `::`.
236
+
`self` may only be used as the first segment of a path (without a preceding `::`) or as the last segment (preceded by `::`).
237
+
238
+
r[paths.qualifiers.mod-self.trailing]
239
+
When `self` appears as the last segment of a path, it refers to the entity named by the preceding segment. The preceding path must resolve to a [module], [enumeration], or [trait].
240
+
241
+
```rust
242
+
modm {
243
+
pubenumE { V1 }
244
+
pubtraitTr {}
245
+
pub(incrate::m::self) fng() {} // OK: Modules can be parents of `self`.
246
+
}
247
+
typeTy=m::E::self; // OK: Enumerations can be parents of `self`.
248
+
fnf<T:m::Tr::self>() {} // OK: Traits can be parents of `self`.
249
+
# fnmain() { let_:Ty=m::E::V1; }
250
+
```
251
+
252
+
```rust,compile_fail,E0223
253
+
struct S;
254
+
type Ty = S::self; // ERROR: Structs cannot be parents of `self`.
255
+
# fn main() {}
256
+
```
257
+
258
+
> [!NOTE]
259
+
> See [items.use.self] for additional rules about `self` in `use` declarations.
237
260
238
261
r[paths.qualifiers.self-pat]
239
262
In a method body, a path which consists of a single `self` segment resolves to the method's self parameter.
@@ -482,6 +505,7 @@ mod without { // crate::without
0 commit comments