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
This attempts to update the `use` chapter to explain the kinds of paths
and syntax it supports in more detail. This is not an exhaustive
explanation of resolution, and there are several things it does not
cover.
* Simultaneously binding a list of paths with a common prefix and their common
26
27
parent module, using the `self` keyword, such as `use a::b::{self, c, d::e};`
27
28
* Rebinding the target name as a new local name, using the syntax `use p::q::r
@@ -87,75 +88,186 @@ In this example, the module `quux` re-exports two public names defined in
87
88
88
89
## `use` Paths
89
90
90
-
> **Note**: This section is incomplete.
91
+
The [paths] that are allowed in a `use` item follow the [_SimplePath_] grammar and are similar to the paths that may be used in an expression.
92
+
They may create bindings for:
91
93
92
-
Some examples of what will and will not work for `use` items:
93
-
<!-- Note: This example works as-is in either 2015 or 2018. -->
94
+
* Nameable [items]
95
+
*[Enum variants]
96
+
*[Built-in types]
97
+
*[Attributes]
98
+
*[Derive macros]
94
99
95
-
```rust
96
-
# #![allow(unused_imports)]
97
-
usestd::path::{self, Path, PathBuf}; // good: std is a crate name
98
-
usecrate::foo::baz::foobaz; // good: foo is at the root of the crate
100
+
They cannot import [associated items], [generic parameters], [local variables], paths with [`Self`], or [tool attributes]. More restrictions are described below.
99
101
100
-
modfoo {
102
+
`use` will create bindings for all [namespaces] from the imported entities, with the exception of a `self` import (described below) which only imports the type namespace.
103
+
For example, the following illustrates creating bindings for the same name in two namespaces:
101
104
102
-
pubmodexample {
103
-
pubmoditer {}
104
-
}
105
-
106
-
usecrate::foo::example::iter; // good: foo is at crate root
107
-
// use example::iter; // bad in 2015 edition: relative paths are not allowed without `self`; good in 2018 edition
108
-
useself::baz::foobaz; // good: self refers to module 'foo'
109
-
usecrate::foo::bar::foobar; // good: foo is at crate root
105
+
```rust
106
+
modstuff {
107
+
pubstructFoo(pubi32);
108
+
}
110
109
111
-
pubmodbar {
112
-
pubfnfoobar() { }
113
-
}
110
+
// Imports the `Foo` type and the `Foo` constructor.
111
+
usestuff::Foo;
114
112
115
-
pubmodbaz {
116
-
usesuper::bar::foobar; // good: super refers to module 'foo'
117
-
pubfnfoobaz() { }
118
-
}
113
+
fnexample() {
114
+
letctor=Foo; // From value namespace
115
+
letx:Foo=ctor(123);
119
116
}
120
-
121
-
fnmain() {}
122
117
```
123
118
124
-
> **Edition Differences**: In the 2015 edition, `use` paths also allow
125
-
> accessing items in the crate root. Using the example above, the following
126
-
> `use` paths work in 2015 but not 2018:
119
+
> **Edition Differences**: In the 2015 edition, `use` paths are relative from the crate root.
120
+
> For example:
127
121
>
128
122
> ```rust,edition2015
129
-
> # mod foo {
130
-
> # pub mod example { pub mod iter {} }
131
-
> # pub mod baz { pub fn foobaz() {} }
132
-
> # }
133
-
> use foo::example::iter;
134
-
> use ::foo::baz::foobaz;
123
+
> mod foo {
124
+
> pub mod example { pub mod iter {} }
125
+
> pub mod baz { pub fn foobaz() {} }
126
+
> }
127
+
> mod bar {
128
+
> // Resolves `foo` from the crate root.
129
+
> use foo::example::iter;
130
+
> // :: prefix explicitly resolves `foo` from the crate root.
131
+
> use ::foo::baz::foobaz;
132
+
> }
133
+
>
135
134
> # fn main() {}
136
135
> ```
137
136
>
138
137
> The 2015 edition does not allow use declarations to reference the [extern prelude].
139
-
> Thus [`extern crate`] declarations are still required in 2015 to
140
-
> reference an external crate in a use declaration. Beginning with the 2018
141
-
> edition, use declarations can specify an external crate dependency the same
142
-
> way `extern crate` can.
143
-
>
144
-
> In the 2018 edition, if an in-scope item has the same name as an external
145
-
> crate, then `use` of that crate name requires a leading `::` to
146
-
> unambiguously select the crate name. This is to retain compatibility with
> use ::std::fs; // Imports from the `std` crate, not the module below.
152
-
> use self::std::fs as self_fs; // Imports the module below.
153
-
>
154
-
> mod std {
155
-
> pub mod fs {}
156
-
> }
157
-
> # fn main() {}
158
-
> ```
138
+
> Thus, [`extern crate`] declarations are still required in 2015 to reference an external crate in a `use` declaration.
139
+
> Beginning with the 2018 edition, `use` declarations can specify an external crate dependency the same way `extern crate` can.
140
+
141
+
## `as` renames
142
+
143
+
The `as` keyword can be used to change the name of an imported entity.
144
+
For example:
145
+
146
+
```rust
147
+
// Creates a non-public alias `bar` for the function `foo`.
148
+
use inner::foo as bar;
149
+
150
+
mod inner {
151
+
pub fn foo() {}
152
+
}
153
+
```
154
+
155
+
## Brace syntax
156
+
157
+
Braces can be used in the last segment of the path to import multiple entities from the previous segment or the current scope if there are no previous segments.
158
+
Braces can be nested, creating a tree of paths, where each grouping of segments is logically combined with its parent to create a full path.
An empty brace does not import anything. `a::b::{}` is treated as `a::b::{self as _}` and `use {};` has no effect.
169
+
170
+
> **Edition Differences**: In the 2015 edition, paths are relative to the crate root, so an import such as `use {foo, bar};` will import the names `foo` and `bar` from the crate root, whereas starting in 2018 those names are relative to the current scope.
171
+
172
+
## `self` imports
173
+
174
+
The keyword `self` may be used in the [brace syntax](#brace-syntax) to create a binding of the parent entity under its own name.
175
+
176
+
```rust
177
+
modstuff {
178
+
pubfnfoo() {}
179
+
pubfnbar() {}
180
+
}
181
+
modexample {
182
+
// Creates a binding for `stuff` and `foo`.
183
+
usecrate::stuff::{self, foo};
184
+
pubfnbaz() {
185
+
foo();
186
+
stuff::bar();
187
+
}
188
+
}
189
+
# fnmain() {}
190
+
```
191
+
192
+
`self` only creates a binding from the [type namespace] of the parent entity.
193
+
For example, in the following, only the `foo` mod is imported:
194
+
195
+
```rust,compile_fail
196
+
mod bar {
197
+
pub mod foo {}
198
+
pub fn foo() {}
199
+
}
200
+
201
+
// This only imports the module `foo`. The function `foo` lives in
202
+
// the value namespace and is not imported.
203
+
use bar::foo::{self};
204
+
205
+
fn main() {
206
+
foo(); // Error, `foo` is a module
207
+
}
208
+
```
209
+
210
+
> **Note**: `self` as the first segment of a `use` path has a different meaning from `self` inside braces.
211
+
> See [`self`] in the paths chapter for more information no the meaning of a leading `self`.
212
+
213
+
## Glob imports
214
+
215
+
The `*` character may be used as the last segment of a `use` path to import all importable entities from the entity of the preceding segment.
216
+
For example:
217
+
218
+
```rust
219
+
// Creates a non-public alias to `bar`.
220
+
usefoo::*;
221
+
222
+
modfoo {
223
+
fni_am_private() {}
224
+
enumExample {
225
+
V1,
226
+
V2,
227
+
}
228
+
pubfnbar() {
229
+
// Creates local aliases to V1 and V2 of the Example enum.
230
+
useExample::*;
231
+
letx=V1;
232
+
}
233
+
}
234
+
```
235
+
236
+
Items and named imports are allowed to shadow names from glob imports in the same [namespace].
237
+
That is, if there is a name already defined by another item in the same namespace, the glob import will skip it.
238
+
For example:
239
+
240
+
```rust
241
+
// This creates a binding to the `clashing::Foo` tuple struct constructor, but
242
+
// does not import its type because that would conflict with the `Foo` struct
243
+
// defined here.
244
+
//
245
+
// Note that the order of definition here is unimportant.
246
+
useclashing::*;
247
+
structFoo {
248
+
field:f32,
249
+
}
250
+
251
+
fndo_stuff() {
252
+
// Uses the constructor from clashing::Foo
253
+
letf1=Foo(123);
254
+
// The struct expression uses the type from the Foo struct defined above.
255
+
letf2=Foo { field:1.0 };
256
+
// Also imported from the glob import.
257
+
letz=Bar {};
258
+
}
259
+
260
+
modclashing {
261
+
pubstructFoo(pubi32);
262
+
pubstructBar {}
263
+
}
264
+
```
265
+
266
+
`*` cannot be used as the first or intermediate segments.
267
+
`*` cannot be used to import a module's contents into itself (such as `use self::*;`).
268
+
269
+
> **Edition Differences**: In the 2015 edition, paths are relative to the crate root, so an import such as `use *;` is valid, and it means to import everything from the crate root.
270
+
> This cannot be used in the crate root itself.
159
271
160
272
## Underscore Imports
161
273
@@ -201,8 +313,100 @@ m!(use std as _;);
201
313
// use std as _;
202
314
```
203
315
204
-
[IDENTIFIER]: ../identifiers.md
316
+
## Restrictions
317
+
318
+
The following are restrictions for valid `use` declarations.
319
+
320
+
*`use crate;` must use `as` to define the name to bind the crate root to.
321
+
*`use {self};` is an error, there must be a leading segment when using `self`.
322
+
* As with any item definition, `use` imports cannot create duplicate bindings of the same name in the same namespace in a module or block.
323
+
*`use` paths with `$crate` are not allowed in a [`macro_rules`] expansion.
324
+
*`use` paths cannot refer to enum variants through a [type alias]. Example:
325
+
```rust,compile_fail
326
+
enum MyEnum {
327
+
MyVariant
328
+
}
329
+
type TypeAlias = MyEnum;
330
+
331
+
use MyEnum::MyVariant; // OK
332
+
use TypeAlias::MyVariant; // ERROR
333
+
```
334
+
335
+
## Ambiguities
336
+
337
+
> **Note**: This section is incomplete.
338
+
339
+
Some situations are an error when there is an ambiguity as to which name a `use` declaration refers to, when there are two name candidates that do not resolve to the same entity.
340
+
341
+
Glob imports are allowed to import conflicting names in the same namespaces as long as the name is not used or shadowed.
342
+
Example:
343
+
344
+
```rust
345
+
modfoo {
346
+
pubstructQux;
347
+
}
348
+
349
+
modbar {
350
+
pubstructQux;
351
+
}
352
+
353
+
usefoo::*;
354
+
usebar::*; // Ok, no name conflict.
355
+
356
+
fnmain() {
357
+
// This would be an error, due to the ambiguity.
358
+
// let x = Qux;
359
+
}
360
+
```
361
+
362
+
Multiple glob imports are allowed to import the same name if the imports are of the same item (following re-exports). The visibility of the name is the maximum visibility of the imports. Example:
363
+
364
+
```rust
365
+
modfoo {
366
+
pubstructQux;
367
+
}
368
+
369
+
modbar {
370
+
pubusesuper::foo::Qux;
371
+
}
372
+
373
+
// These both import the same `Qux`.
374
+
// The visibility of `Qux` is `pub` because that is the maximum
375
+
// visibility between these two `use` declarations.
376
+
usefoo::*;
377
+
pubusebar::*;
378
+
# fnmain() {}
379
+
```
380
+
381
+
If an in-scope item has the same name as a crate name in the [extern prelude], then `use` of that crate name requires a leading `::` to unambiguously select the crate name or `crate::` to use the item from the crate root.
0 commit comments