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
+94-21Lines changed: 94 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -200,6 +200,9 @@ mod example {
200
200
# fnmain() {}
201
201
```
202
202
203
+
> [!NOTE]
204
+
> `self` may also be used as the first segment of a path. The usage 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
+
203
206
r[items.use.self.namespace]
204
207
`self` only creates a binding from the [type namespace] of the parent entity. For example, in the following, only the `foo` mod is imported:
205
208
@@ -218,9 +221,6 @@ fn main() {
218
221
}
219
222
```
220
223
221
-
> [!NOTE]
222
-
> `self` may also be used as the first segment of a path. The usage 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`.
223
-
224
224
r[items.use.glob]
225
225
## Glob imports
226
226
@@ -333,32 +333,105 @@ m!(use std as _;);
333
333
r[items.use.restrictions]
334
334
## Restrictions
335
335
336
-
The following are restrictions for valid `use` declarations:
336
+
The following rules are restrictions for valid `use` declarations.
337
+
338
+
r[items.use.restrictions.crate-alias]
339
+
When using `crate` to import the current crate, you must use `as` to define the binding name.
340
+
341
+
> [!EXAMPLE]
342
+
> ```rust
343
+
> usecrateas root;
344
+
> usecrate::{selfas root2};
345
+
>
346
+
> // Not allowed:
347
+
> // use crate;
348
+
> // use crate::{self};
349
+
> ```
337
350
338
-
r[items.use.restrictions.crate]
339
-
*`use crate;` must use `as` to define the name to which to bind the crate root.
351
+
r[items.use.restrictions.macro-crate-alias]
352
+
Whenusing [`$crate`] inamacrotranscribertoimportthecurrentcrate, youmustuse `as` to define the binding name.
353
+
354
+
> [!EXAMPLE]
355
+
> ```rust
356
+
> macro_rules! import_crate_root {
357
+
> () => {
358
+
> use$crateas my_crate;
359
+
> use$crate::{selfas my_crate2};
360
+
> };
361
+
> }
362
+
> ```
340
363
341
-
r[items.use.restrictions.self]
342
-
*`use {self};` is an error; there must be a leading segment when using `self`.
364
+
r[items.use.restrictions.self-alias]
365
+
Whenusing `self` toimportthecurrentmodule, youmustuse `as` to define the binding name.
343
366
344
-
r[items.use.restrictions.duplicate-name]
345
-
* As with any item definition, `use` imports cannot create duplicate bindings of the same name in the same namespace in a module or block.
367
+
> [!EXAMPLE]
368
+
> ```rust
369
+
> use {selfas this_module};
370
+
> useselfas this_module2;
371
+
> useself::{selfas this_module3};
372
+
>
373
+
> // Not allowed:
374
+
> // use {self};
375
+
> // use self;
376
+
> // use self::{self};
377
+
> ```
346
378
347
-
r[items.use.restrictions.macro-crate]
348
-
*`use` paths with `$crate` are not allowed in a [`macro_rules`] expansion.
379
+
r[items.use.restrictions.super-alias]
380
+
Whenusing `super` toimportaparentmodule, youmustuse `as` to define the binding name.
0 commit comments