docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

#5.5. use (imports)

On this page

The use keyword (keywords) brings items of other modules into the visible name space of the current module.

#5.5.1. Basic form

use_stmt    ::= "use" use_tree ";"
use_tree    ::= path "::" "*"
              | path "::" "**"
              | path "::" "{" use_tree ("," use_tree)* ","? "}"
              | path ("as" identifier)?
path        ::= path_root ("::" identifier)*
path_root   ::= "pkg" | "sup" | identifier

Globs (*, **) take no as alias: they import many items at once, so a single alias has no meaning. path ("as" identifier)? therefore applies only to single-item imports.

nabi
use foo::bar;
use std::time::SystemTime;
use pkg::geom::Point;

The basic structure is use, a path, and a closing ;. The last identifier of the path becomes the item's name in the current module. After the use, the item can be referred to by short names such as bar, SystemTime, Point.

#5.5.2. Multiple imports: {...}

Several items with the same prefix can be grouped on one line.

nabi
use foo::{a, b, c};
use std::time::{SystemTime, UNIX_EPOCH};

Each element of {..} continues the group's resolved prefix. The prefix may be empty (pkg::{..}, sup::{..} reaching the root). Thus use p::{a, b} is, in any module, exactly:

nabi
use foo::a;
use foo::b;
  • Groups may nest and may contain *, **, and as (use foo::{bar::{x, y as z}, baz::*};).
  • self inside a group is an error.
  • Each leaf must resolve to an item, an enum variant, or a module. Otherwise it is an "unresolved import" error at that leaf.

#5.5.3. Aliases: as

The as keyword renames an imported item.

nabi
use foo::very_long_name as short;
use core::collections::Dict as Map;

A new name follows as; in the current module only the new name is visible, and the original name is not.

It can also be used within a multiple import.

nabi
use foo::{a as alpha, b, c as charlie};

#5.5.4. Glob: * (shallow)

Imports all visible items of a module at once.

nabi
use foo::*;                    // all visible items of foo

Rules: * must be the last token, so a form such as use foo::*::bar is an ERROR. Only items whose visibility the caller can see are imported (visibility): items that are pub, or pub(pkg) within the same package, and so on, whose reach includes the caller. Only the direct children of foo are imported; items of submodules are not (that is the role of **). A glob use in a block also reaches the headers of the items declared in that block (3.9.1). An item the module defines wins over a glob-imported item of the same name: the glob gives way, and nothing is reported (a named import of that name is an error instead, 5.5.12).

#5.5.5. Recursive glob: ** (deep)

Imports recursively, including submodules.

nabi
use foo::**;                   // all items of foo + all items of foo's child modules + ...

It imports every visible item through submodules at once, so its scope is much wider than the shallow glob (*). It is often used as the prelude form of a large library. A recursive glob imports module items and enum variants, never the associated items of a type: under use core::time::**;, a bare MAX does not name Duration::MAX.

#5.5.6. Glob / recursive glob conflicts

When several globs import the same name, a conflict occurs.

nabi
use foo::*;
use bar::*;
// if both have a name 'item', that name is ambiguous and using it is an ERROR.

Resolve it by importing one explicitly or by giving an alias.

nabi
use foo::item;                 // explicit: only foo::item is visible
use bar::item as bar_item;      // alias: both under different names

#5.5.7. Path starting points

The first identifier of a path follows the rules of paths, in three cases. pkg:: starts from the current package's root. sup:: starts from the parent module, and can be chained as sup::sup::... to go up several levels. Otherwise an ordinary identifier is usable when it is a name currently visible (a child module, an already used name, etc.). Since a child of the same module is reachable by its identifier alone, there is no separate self::.

A path's first identifier names a module or a type, so a function or constant imported under a module's name does not take the module's place: with mod norm;, pub use norm::norm; imports the function norm, and a later norm::other still starts at the module.

nabi
mod norm;                       // holds `pub fn norm` and `pub fn other`
pub use norm::{norm, other};    // the function `norm` and `other`
let a = norm::norm(-2.0);       // the module `norm`

#Importing enum variants

The variants of an enum are items under that enum, so they can be imported with use. A use path may also start with a currently visible type name.

nabi
use Shape::{Circle};                // a single variant
use Shape::{Square as Sq};          // alias
use Shape::*;                       // all variants
use Result::*;                      // starts with a visible type name

Without a use, a variant is bare where the expected type is its enum (2.4.8): Ok / Err in a function that returns a Result, and in a match on a Result. When a bare name in a pattern resolves to an imported variant or a variant of the scrutinee's enum, the name matches that variant rather than creating a new binding.

nabi
match r {
    Ok(v)  => v,
    Err(e) => fallback(e),
}

#5.5.8. Where use may appear

use may appear at module level or at function body level.

nabi
// module level: visible throughout that module
use foo::bar;

fn f() {
    // inside a function: visible only within that function
    use foo::baz;
    let x = baz();
}

A use inside a function is a local import of that function and is not exposed outside. A visibility keyword (pub) therefore has no meaning there. The house style keeps every use at the top of the file: the warning lint local_use (6.2) reports one inside a function body.

#5.5.9. Visibility of use

nabi
pub use foo::bar;               // exposed outside (re-export)
pub(pkg) use foo::bar;          // exposed only within the same package

use foo::bar;                   // module-private: visible only within the same module

pub use is a re-export. Details are defined in visibility.

#5.5.10. Automatic import of core

The compiler implicitly applies use core::prelude::*; at the start of every Nabi source outside the standard library. The prelude is a fixed list of core names (stdlib 0.2.3): the top-level core modules, the common collection and core types, the traits written in impl headers and bounds, and the printing and panic functions. Other core items are named through a prelude module (task::sleep, ops::Add) or imported. The implicit import comes last in lookup (5.7.1), so a module's own items and uses shadow it. #![no_implicit_nabi] turns it off for a file.

bare (the hazardous area) and std (target-dependent), the two tiers besides core, are excluded from automatic visibility and require an explicit import. Details are defined in standard library tiers.

Every module outside the standard library (external packages, user libraries) also requires an explicit import. A package's module named prelude is not pulled in automatically. It is merely a conventional name chosen by the library author, so users must import it explicitly, as in use my_lib::prelude::*;.

nabi
// library side
pub mod prelude {
    pub use pkg::api::core::*;
    pub use pkg::api::ext::*;
}

// user side
use my_lib::prelude::*;            // visible only when imported explicitly

#5.5.11. Importing the same module / item twice: allowed

nabi
use foo::bar;
use foo::bar;                   // OK: duplicate import is harmless

A duplicate import of the same item has no side effects and is allowed. When an identity check confirms it is truly the same item, it passes.

#5.5.12. Same name from different modules: conflict

nabi
use foo::Item;
use bar::Item;                  // ERROR: 'Item' already imported

Importing the same short name from different modules is a conflict. Resolve it by aliasing one.

Functions, constants and statics form one namespace, and modules and types another, as where items are declared: a function and a module of the same name are no conflict (use core::channel; beside use core::channel::channel;).

nabi
use foo::Item;
use bar::Item as BarItem;       // OK

A named import (use a::f;, use a::{f}, or use a::f as g; for g) and an item of the same name defined in the module, in a namespace the import fills, are a compile error (E0032, 9.9.4): the item would hide the import unseen. Import it under another name (use a::f as a_f;) or remove the use. A type import beside a value item of its name does not collide, and a glob import gives way to the module's items (5.5.4).

nabi
use shapes::square;
fn square(x: f64) -> f64 { x * x }    // ERROR: `square` is both imported from `shapes::square` and defined in this module

#5.5.13. Evaluation / effect of use

use changes only compile-time name resolution information and has no direct effect on runtime behavior. use itself executes no code; it only adds an alias to the current module's name table.

#5.5.14. Importing a module itself

nabi
use foo::geom;                  // imports the module foo::geom itself
let p = geom::Point { ... };    // use geom as a short name

Modules themselves, not only items, can be imported. The module's name (the last identifier) then becomes visible as a short name in the current module; this is commonly used to access items through a child module.

Found a mistake? Edit this page on GitHub. Last updated