@@ -57,6 +57,8 @@ fn main() {
5757}
5858```
5959
60+ ## ` use ` Visibility
61+
6062Like items, ` use ` declarations are private to the containing module, by
6163default. Also like items, a ` use ` declaration can be public, if qualified by
6264the ` pub ` keyword. Such a ` use ` declaration serves to _ re-export_ a name. A
@@ -82,32 +84,32 @@ mod quux {
8284In this example, the module ` quux ` re-exports two public names defined in
8385` foo ` .
8486
85- Also note that the paths contained in ` use ` items are relative to the crate
86- root. So, in the previous example, the ` use ` refers to ` quux::foo::{bar, baz} ` ,
87- and not simply to ` foo::{bar, baz} ` . This also means that top-level module
88- declarations should be at the crate root if direct usage of the declared
89- modules within ` use ` items is desired. It is also possible to use ` self ` and
90- ` super ` at the beginning of a ` use ` item to refer to the current and direct
91- parent modules respectively. All rules regarding accessing declared modules in
92- ` use ` declarations apply to both module declarations and ` extern crate `
93- declarations.
87+ ## ` use ` Paths
88+
89+ Paths in ` use ` items must start with a crate name or one of the [ path
90+ qualifiers] ` crate ` , ` self ` , ` super ` , or ` :: ` . ` crate ` refers to the current
91+ crate. ` self ` refers to the current module. ` super ` refers to the parent
92+ module. ` :: ` can be used to explicitly refer to a crate, requiring an extern
93+ crate name to follow.
9494
9595An example of what will and will not work for ` use ` items:
96+ <!-- Note: This example works as-is in either 2015 or 2018. -->
9697
9798``` rust
9899# #![allow(unused_imports)]
99- use foo :: baz :: foobaz; // good: foo is at the root of the crate
100+ use std :: path :: {self , Path , PathBuf }; // good: std is a crate name
101+ use crate :: foo :: baz :: foobaz; // good: foo is at the root of the crate
100102
101103mod foo {
102104
103105 mod example {
104106 pub mod iter {}
105107 }
106108
107- use foo :: example :: iter; // good: foo is at crate root
108- // use example::iter; // bad: example is not at the crate root
109+ use crate :: foo :: example :: iter; // good: foo is at crate root
110+ // use example::iter; // bad: relative paths are not allowed without `self`
109111 use self :: baz :: foobaz; // good: self refers to module 'foo'
110- use foo :: bar :: foobar; // good: foo is at crate root
112+ use crate :: foo :: bar :: foobar; // good: foo is at crate root
111113
112114 pub mod bar {
113115 pub fn foobar () { }
@@ -122,6 +124,33 @@ mod foo {
122124fn main () {}
123125```
124126
127+ > ** Edition Differences** : In the 2015 Edition, ` use ` paths also allow
128+ > accessing items in the crate root. Using the example above, the following
129+ > ` use ` paths work in 2015 but not 2018:
130+ >
131+ > ``` rust,ignore
132+ > use foo::example::iter;
133+ > use ::foo::baz::foobaz;
134+ > ```
135+ >
136+ > In the 2018 Edition, if an in-scope item has the same name as an external
137+ > crate, then `use` of that crate name requires a leading `::` to
138+ > unambiguously select the crate name. This is to retain compatibility with
139+ > potential future changes. <!-- uniform_paths future-proofing -->
140+ >
141+ > ```rust,edition2018
142+ > // use std::fs; // Error, this is ambiguous.
143+ > use ::std::fs; // Imports from the `std` crate, not the module below.
144+ > use self::std::fs as self_fs; // Imports the module below.
145+ >
146+ > mod std {
147+ > pub mod fs {}
148+ > }
149+ > # fn main() {}
150+ > ```
151+
152+
125153[IDENTIFIER]: identifiers.html
126154[_SimplePath_]: paths.html#simple-paths
127155[_Visibility_]: visibility-and-privacy.html
156+ [path qualifiers]: paths.html#path-qualifiers
0 commit comments