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
+25-1Lines changed: 25 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,7 +182,7 @@ r[items.use.self]
182
182
## `self` imports
183
183
184
184
r[items.use.self.intro]
185
-
The keyword `self` may be used within [brace syntax](#brace-syntax) to create a binding of the parent entity under its own name.
185
+
The keyword `self` may be used within [brace syntax] to create a binding of the parent entity under its own name.
186
186
187
187
```rust
188
188
modstuff {
@@ -203,6 +203,26 @@ 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.module]
207
+
When `self` is used within [brace syntax], the path preceding the brace group must resolve to a [module], [enumeration], or [trait].
208
+
209
+
```rust
210
+
modm {
211
+
pubenumE { V1, V2 }
212
+
pubtraitTr { fnf(&self); }
213
+
}
214
+
usem::{selfas _}; // OK: Modules can be parents of `self`.
215
+
usem::E::{self, V1}; // OK: Enums can be parents of `self`.
216
+
usem::Tr::{self}; // OK: Traits can be parents of `self`.
217
+
# fnmain() {}
218
+
```
219
+
220
+
```rust,compile_fail,E0432
221
+
struct S {}
222
+
use S::{self as _}; // ERROR: Structs cannot be parents of `self`.
223
+
# fn main() {}
224
+
```
225
+
206
226
r[items.use.self.namespace]
207
227
`self` only creates a binding from the [type namespace] of the parent entity. For example, in the following, only the `foo` mod is imported:
0 commit comments