官术网_书友最值得收藏!

File as a module

Modules can also be created as files. For instance, for a main.rs file in a directory named foo, we can create a module bar as a file in the same directory as foo/bar.rs. Then in main.rs, we need to tell the compiler of this module, that is, declare the module with mod foo;. This is an extra step when using file-based modules. To demonstrate using a file as a module, we have created a directory named modules_demo, which has the following structure:

+ modules_demo
└── foo.rs
└── main.rs

Our foo.rs contains a struct Bar, with its impl block:

// modules_demo/foo.rs

pub struct Bar;

impl Bar {
pub fn init() {
println!("Bar type initialized");
}
}

We want to use this module in main.rs. Our main.rs, has the following code:

// modules_demo/main.rs

mod foo;

use crate::foo::Bar;

fn main() {
let _bar = Bar::init();
}

We declare our module, foo, using mod foo;. We then use the Bar struct from the module by writing use crate::foo::Bar. Notice the crate prefix in use crate::foo::Bar; here. There are three ways to use an item from a module depending on the prefix you use:

Absolute imports:

  • crate: An absolute import prefix that refers to the the current crate's root. In the preceding code, this would be the root module, that is, main.rs file. Anything after the crate keyword is resolved from the root module.

Relative imports:

  • self: A relative import prefix that refers to an item relative from the current module. This is used when any code wants to refer to its containing module, for example, use self::foo::Bar;. This is mostly used when re-exporting items from a child module to be available from the parent module.
  • super: A relative import prefix that can use and import an item from the parent module. A child module such as the tests module would use this to import items from the parent module. For example, if a module bar wants to access an item Foo from its parent module foo, it would import it as use super::foo::Foo; in module bar.

The third way to create modules, is to organize them as directories.

主站蜘蛛池模板: 卢氏县| 民勤县| 武隆县| 威信县| 崇仁县| 武城县| 镇江市| 桃园市| 衢州市| 永平县| 襄樊市| 莎车县| 兴国县| 华亭县| 正定县| 景德镇市| 黎平县| 安图县| 湘潭县| 渭南市| 冀州市| 丹东市| 简阳市| 凤凰县| 清流县| 龙海市| 新河县| 平舆县| 灌南县| 九江市| 南皮县| 察雅县| 邵东县| 镇赉县| 苗栗县| 揭西县| 河曲县| 都兰县| 隆昌县| 平阳县| 吉安市|