Skip to content

Commit 442cbef

Browse files
authored
Merge pull request #2136 from mu001999-contrib/doc/import-path-kw
Document importing path-segment keyword
2 parents 44b7d3f + f9c4187 commit 442cbef

2 files changed

Lines changed: 98 additions & 21 deletions

File tree

src/items/use-declarations.md

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ mod example {
200200
# fn main() {}
201201
```
202202

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+
203206
r[items.use.self.namespace]
204207
`self` only creates a binding from the [type namespace] of the parent entity. For example, in the following, only the `foo` mod is imported:
205208

@@ -218,9 +221,6 @@ fn main() {
218221
}
219222
```
220223

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-
224224
r[items.use.glob]
225225
## Glob imports
226226

@@ -333,32 +333,105 @@ m!(use std as _;);
333333
r[items.use.restrictions]
334334
## Restrictions
335335

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+
> use crate as root;
344+
> use crate::{self as root2};
345+
>
346+
> // Not allowed:
347+
> // use crate;
348+
> // use crate::{self};
349+
> ```
337350
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+
When using [`$crate`] in a macro transcriber to import the current crate, you must use `as` to define the binding name.
353+
354+
> [!EXAMPLE]
355+
> ```rust
356+
> macro_rules! import_crate_root {
357+
> () => {
358+
> use $crate as my_crate;
359+
> use $crate::{self as my_crate2};
360+
> };
361+
> }
362+
> ```
340363
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+
When using `self` to import the current module, you must use `as` to define the binding name.
343366
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 {self as this_module};
370+
> use self as this_module2;
371+
> use self::{self as this_module3};
372+
>
373+
> // Not allowed:
374+
> // use {self};
375+
> // use self;
376+
> // use self::{self};
377+
> ```
346378
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+
When using `super` to import a parent module, you must use `as` to define the binding name.
381+
382+
> [!EXAMPLE]
383+
> ```rust
384+
> mod a {
385+
> mod b {
386+
> use super as parent;
387+
> use super::{self as parent2};
388+
> use self::super as parent3;
389+
> use super::super as grandparent;
390+
> use super::super::{self as grandparent2};
391+
>
392+
> // Not allowed:
393+
> // use super;
394+
> // use super::{self};
395+
> // use self::super;
396+
> // use super::super;
397+
> // use super::super::{self};
398+
> }
399+
> }
400+
> ```
401+
402+
r[items.use.restrictions.extern-prelude]
403+
`::` as the [extern prelude] cannot be imported.
404+
405+
> [!EXAMPLE]
406+
> ```rust,edition2018,compile_fail
407+
> use ::{self as root}; //~ Error
408+
> ```
409+
410+
> [!EDITION-2018]
411+
> In the 2015 edition, the prefix `::` refers to the crate root, so `use ::{self as root};` is allowed because it is same as `use crate::{self as root};`. Starting with the 2018 edition the `::` prefix refers to the extern prelude, which cannot be directly imported.
412+
>
413+
> ```rust,edition2015
414+
> use ::{self as root}; //~ Ok
415+
> ```
416+
417+
r[items.use.restrictions.duplicate-name]
418+
As with any item definition, `use` imports cannot create duplicate bindings of the same name in the same namespace in a module or block.
349419
350420
r[items.use.restrictions.variant]
351-
* `use` paths cannot refer to enum variants through a [type alias]. For example:
352-
```rust,compile_fail
353-
enum MyEnum {
354-
MyVariant
355-
}
356-
type TypeAlias = MyEnum;
421+
`use` paths cannot refer to enum variants through a [type alias].
357422
358-
use MyEnum::MyVariant; //~ OK
359-
use TypeAlias::MyVariant; //~ ERROR
360-
```
423+
> [!EXAMPLE]
424+
> ```rust,compile_fail
425+
> enum MyEnum {
426+
> MyVariant
427+
> }
428+
> type TypeAlias = MyEnum;
429+
>
430+
> use MyEnum::MyVariant; //~ OK
431+
> use TypeAlias::MyVariant; //~ ERROR
432+
> ```
361433
434+
[`$crate`]: paths.qualifiers.macro-crate
362435
[Attributes]: ../attributes.md
363436
[Built-in types]: ../types.md
364437
[Derive macros]: macro.proc.derive

src/paths.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ r[paths.qualifiers]
188188

189189
Paths can be denoted with various leading qualifiers to change the meaning of how it is resolved.
190190

191+
> [!NOTE]
192+
> [`use` declarations] have additional behaviors and restrictions for `self`, `super`, `crate`, and `$crate`.
193+
191194
r[paths.qualifiers.global-root]
192195
### `::`
193196

@@ -486,5 +489,6 @@ mod without { // crate::without
486489
[traits]: items/traits.md
487490
[types]: types.md
488491
[union]: items/unions.md
492+
[`use` declarations]: items/use-declarations.md
489493
[value namespace]: names/namespaces.md
490494
[visibility]: visibility-and-privacy.md

0 commit comments

Comments
 (0)