The imports of a crate/module should consist of the following sections, in order, with a blank space between each:
extern crate
directivesuse
importsuse
importspub use
importsFor example:
fn main() { // Crates. extern crate getopts; extern crate mylib; // Standard library imports. use getopts::{optopt, getopts}; use std::os; // Import from a library that we wrote. use mylib::webserver; // Will be reexported when we import this module. pub use self::types::Webdata; }// Crates. extern crate getopts; extern crate mylib; // Standard library imports. use getopts::{optopt, getopts}; use std::os; // Import from a library that we wrote. use mylib::webserver; // Will be reexported when we import this module. pub use self::types::Webdata;
use *
, except in tests.Glob imports have several downsides: * They make it harder to tell where names are bound. * They are forwards-incompatible, since new upstream exports can clash with existing names.
When writing a test
submodule, importing super::*
is appropriate
as a convenience.
For example:
fn main() { use option::Option; use mem; let i: isize = mem::transmute(Option(0)); }use option::Option; use mem; let i: isize = mem::transmute(Option(0));
[FIXME] Add rationale.