3 > **<sup>Syntax:<sup>**
5 > `use` [_SimplePath_] (`as` [IDENTIFIER])<sup>?</sup> `;`
6 > | `use` ([_SimplePath_]<sup>?</sup> `::`)<sup>?</sup> `{` _UseDeclarationItems_ `}` `;`
7 > | `use` ([_SimplePath_]<sup>?</sup> `::`)<sup>?</sup> `*` `;`
9 > _UseDeclarationItems_ :
10 > _UseDeclarationItem_ ( `,` _UseDeclarationItem_ )<sup>*</sup> `,`<sup>?<sup>
12 > _UseDeclarationItem_ :
13 > ( `self` | [IDENTIFIER] ) ( `as` [IDENTIFIER] )<sup>?</sup>
15 A _use declaration_ creates one or more local name bindings synonymous with
16 some other [path]. Usually a `use` declaration is used to shorten the path
17 required to refer to a module item. These declarations may appear in [modules]
18 and [blocks], usually at the top.
21 [modules]: items/modules.html
22 [blocks]: expressions/block-expr.html
24 > **Note**: Unlike in many languages, `use` declarations in Rust do *not*
25 > declare linkage dependency with external crates. Rather, [`extern crate`
26 > declarations](items/extern-crates.html) declare linkage dependencies.
28 Use declarations support a number of convenient shortcuts:
30 * Simultaneously binding a list of paths differing only in their final element,
31 using the glob-like brace syntax `use a::b::{c,d,e,f};`
32 * Simultaneously binding a list of paths differing only in their final element
33 and their immediate parent module, using the `self` keyword, such as `use
35 * Rebinding the target name as a new local name, using the syntax `use p::q::r
36 as x;`. This can also be used with the last two features: `use a::b::{self as
38 * Binding all paths matching a given prefix, using the asterisk wildcard syntax
41 An example of `use` declarations:
44 use std::option::Option::{Some, None};
45 use std::collections::hash_map::{self, HashMap};
48 fn bar(map1: HashMap<String, usize>, map2: hash_map::HashMap<String, usize>){}
51 // Equivalent to 'foo(vec![std::option::Option::Some(1.0f64),
52 // std::option::Option::None]);'
53 foo(vec![Some(1.0f64), None]);
55 // Both `hash_map` and `HashMap` are in scope.
56 let map1 = HashMap::new();
57 let map2 = hash_map::HashMap::new();
62 Like items, `use` declarations are private to the containing module, by
63 default. Also like items, a `use` declaration can be public, if qualified by
64 the `pub` keyword. Such a `use` declaration serves to _re-export_ a name. A
65 public `use` declaration can therefore _redirect_ some public name to a
66 different target definition: even a definition with a private canonical path,
67 inside a different module. If a sequence of such redirections form a cycle or
68 cannot be resolved unambiguously, they represent a compile-time error.
70 An example of re-exporting:
75 pub use quux::foo::{bar, baz};
84 In this example, the module `quux` re-exports two public names defined in
87 Also note that the paths contained in `use` items are relative to the crate
88 root. So, in the previous example, the `use` refers to `quux::foo::{bar, baz}`,
89 and not simply to `foo::{bar, baz}`. This also means that top-level module
90 declarations should be at the crate root if direct usage of the declared
91 modules within `use` items is desired. It is also possible to use `self` and
92 `super` at the beginning of a `use` item to refer to the current and direct
93 parent modules respectively. All rules regarding accessing declared modules in
94 `use` declarations apply to both module declarations and `extern crate`
97 An example of what will and will not work for `use` items:
100 # #![allow(unused_imports)]
101 use foo::baz::foobaz; // good: foo is at the root of the crate
109 use foo::example::iter; // good: foo is at crate root
110 // use example::iter; // bad: example is not at the crate root
111 use self::baz::foobaz; // good: self refers to module 'foo'
112 use foo::bar::foobar; // good: foo is at crate root
119 use super::bar::foobar; // good: super refers to module 'foo'
127 [IDENTIFIER]: identifiers.html
128 [_SimplePath_]: paths.html