#![allow(unused_imports)] use std::path::{self, Path, PathBuf}; // good: std is a crate name use crate::foo::baz::foobaz; // good: foo is at the `root` of the crate
mod foo {
pubmod example { pubmod iter {} }
use crate::foo::example::iter; // good: foo is at crate root // use example::iter; // bad in 2015 edition: relative paths are not allowed without `self`; good in 2018 edition use self::baz::foobaz; // good: self refers to module 'foo' use crate::foo::bar::foobar; // good: foo is at crate root
pubmod bar { pubfnfoobar() { } }
pubmod baz { use super::bar::foobar; // good: super refers to module 'foo' pubfnfoobaz() { } } }
fnmain() {}
在2018版之后, 如果同一命名空间下与标准库有名字冲突的, 可以加上::来避免歧义:
1 2 3 4 5 6
use ::std::fs; // Imports from the `std` crate, not the module below. use self::std::fs as self_fs; // Imports the module below.
mod std { pubmod fs {} }
4. Underscore Imports
use path as _
使用下划线, 可以将引入的name隐藏, 当导入的是一个trait时, 当前命名空间中所有(在use path as _之后)的struct都能使用Zoo这个trait的方法(事实上, 那个Zoo struct命名为A B C D 都无所谓, 它都能访问zoo这个函数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mod foo { pubtraitZoo { fnzoo(&self) {} }
impl<T> Zoo forT {} }
use self::foo::Zoo as _; structZoo; // Underscore import avoids name conflict with this item.