Use declarations
UseDeclaration -> `use` UseTree `;`
UseTree ->
(SimplePath? `::`)? `*`
| (SimplePath? `::`)? `{` (UseTree ( `,` UseTree )* `,`?)? `}`
| SimplePath ( `as` ( IDENTIFIER | `_` ) )?
A use declaration creates one or more local name bindings synonymous with some other path. Usually a use declaration is used to shorten the path required to refer to a module item. These declarations may appear in modules and blocks, usually at the top. A use declaration is also sometimes called an import, or, if it is public, a re-export.
Use declarations support a number of convenient shortcuts:
- Simultaneously binding a list of paths with a common prefix, using the brace syntax
use a::b::{c, d, e::f, g::h::i};
- Simultaneously binding a list of paths with a common prefix and their common parent module, using the
selfkeyword, such asuse a::b::{self, c, d::e};
- Rebinding the target name as a new local name, using the syntax
use p::q::r as x;. This can also be used with the last two features:use a::b::{self as ab, c as abc}.
- Binding all paths matching a given prefix, using the asterisk wildcard syntax
use a::b::*;.
- Nesting groups of the previous features multiple times, such as
use a::b::{self as ab, c, d::{*, e::f}};
An example of use declarations:
use ;
use Visibility
Like items, use declarations are private to the containing module, by default. Also like items, a use declaration can be public, if qualified by the pub keyword. Such a use declaration serves to re-export a name. A public use declaration can therefore redirect some public name to a different target definition: even a definition with a private canonical path, inside a different module.
If a sequence of such redirections form a cycle or cannot be resolved unambiguously, they represent a compile-time error.
An example of re-exporting:
In this example, the module quux re-exports two public names defined in foo.
use Paths
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. They may create bindings for:
They cannot import associated items, generic parameters, local variables, paths with Self, or tool attributes. More restrictions are described below.
use will create bindings for all namespaces from the imported entities, with the exception that a self import will only import from the type namespace (as described below). For example, the following illustrates creating bindings for the same name in two namespaces:
// Imports the `Foo` type and the `Foo` constructor.
use Foo;
2018 Edition differences
In the 2015 edition,
usepaths are relative to the crate root. For example:#The 2015 edition does not allow use declarations to reference the extern prelude. Thus,
extern cratedeclarations are still required in 2015 to reference an external crate in ausedeclaration. Beginning with the 2018 edition,usedeclarations can specify an external crate dependency the same wayextern cratecan.
as renames
The as keyword can be used to change the name of an imported entity. For example:
// Creates a non-public alias `bar` for the function `foo`.
use foo as bar;
Brace syntax
Braces can be used in the last segment of the path to import multiple entities from the previous segment, or, if there are no previous segments, from the current scope. 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.
// Creates bindings to:
// - `std::collections::BTreeSet`
// - `std::collections::hash_map`
// - `std::collections::hash_map::HashMap`
use ;
An empty brace does not import anything, though the leading path is validated that it is accessible.
2018 Edition differences
In the 2015 edition, paths are relative to the crate root, so an import such as
use {foo, bar};will import the namesfooandbarfrom the crate root, whereas starting in 2018, those names are relative to the current scope.
self imports
The keyword self may be used within brace syntax to create a binding of the parent entity under its own name.
#
Note
selfmay also be used as the first segment of a path. The use ofselfas the first segment and inside ausebrace is logically the same; it means the current module of the parent segment, or the current module if there is no parent segment. Seeselfin the paths chapter for more information on the meaning of a leadingself.
self may appear as the last segment of a use path, preceded by ::. A path of the form P::self is equivalent to P::{self}, and P::self as name is equivalent to P::{self as name}.
use self as _; // Equivalent to `use m::{self as _};`.
use self; // Equivalent to `use m::E::{self};`.
#
Note
See paths.qualifiers.mod-self.trailing for restrictions on the preceding path.
When self is used within brace syntax, the path preceding the brace group must resolve to a module, enumeration, or trait.
use ; // OK: Modules can be parents of `self`.
use ; // OK: Enums can be parents of `self`.
use ; // OK: Traits can be parents of `self`.
#
use ; // ERROR: Structs cannot be parents of `self`.
#
self only creates a binding from the type namespace of the parent entity. For example, in the following, only the foo mod is imported:
// This only imports the module `foo`. The function `foo` lives in
// the value namespace and is not imported.
use ;
Glob imports
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. For example:
// Creates a non-public alias to `bar`.
use *;
Items and named imports are allowed to shadow names from glob imports in the same namespace. That is, if there is a name already defined by another item in the same namespace, the glob import will be shadowed. For example:
// This creates a binding to the `clashing::Foo` tuple struct
// constructor, but does not import its type because that would
// conflict with the `Foo` struct defined here.
//
// Note that the order of definition here is unimportant.
use *;
Note
For areas where shadowing is not allowed, see name resolution ambiguities.
* cannot be used as the first or intermediate segments.
* cannot be used to import a module's contents into itself (such as use self::*;).
2018 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. This cannot be used in the crate root itself.
Underscore imports
Items can be imported without binding to a name by using an underscore with the form use path as _. This is particularly useful to import a trait so that its methods may be used without importing the trait's symbol, for example if the trait's symbol may conflict with another symbol. Another example is to link an external crate without importing its name.
use Zoo as _;
; // Underscore import avoids name conflict with this item.
Asterisk glob imports will import items imported with _ in their unnameable form.
The unique, unnameable symbols are created after macro expansion so that macros may safely emit multiple references to _ imports. For example, the following should not produce an error:
m!;
// This expands to:
// use std as _;
// use std as _;
Restrictions
The following rules are restrictions for valid use declarations.
When using crate to import the current crate, you must use as to define the binding name.
Example
use crate as root; use crate::; // Not allowed: // use crate; // use crate::{self};
When using $crate in a macro transcriber to import the current crate, you must use as to define the binding name.
Example
When using self to import the current module, you must use as to define the binding name.
Example
use ; use self as this_module2; use ; // Not allowed: // use {self}; // use self; // use self::{self};
When using super to import a parent module, you must use as to define the binding name.
Example
:: as the extern prelude cannot be imported.
Example
use ::; //~ Error
2018 Edition differences
In the 2015 edition, the prefix
::refers to the crate root, souse ::{self as root};is allowed because it is same asuse crate::{self as root};. Starting with the 2018 edition the::prefix refers to the extern prelude, which cannot be directly imported.use ::; //~ Ok
As with any item definition, use imports cannot create duplicate bindings of the same name in the same namespace in a module or block.
use paths cannot refer to enum variants through a type alias.
Example
type TypeAlias = MyEnum; use MyVariant; //~ OK use MyVariant; //~ ERROR