File hierarchy

Modules can be mapped to a file/directory hierarchy.

$ tree .
.
|-- my
|   |-- inaccessible.rs
|   |-- mod.rs
|   `-- nested.rs
`-- split.rs

// split.rs
// 这里的声明将会寻找一个文件`my.rs`或者`my/mod.rs`
// 并且将该文件的内容插入到当前的运行环境
mod my;

fn function() {
    println!("called `function()`");
}

fn main() {
    my::function();

    function();

    my::indirect_access();

    my::nested::function();
}

// my/mod.rs
// `mod inaccessible` and `mod nested`都或定位文件`nested.rs`
// 和`inaccessible.rs`,并将它们的所有内容插入到各自的模块命名里面
mod inaccessible;
pub mod nested;

pub fn function() {
    println!("called `my::function()`");
}

fn private_function() {
    println!("called `my::private_function()`");
}

pub fn indirect_access() {
    print!("called `my::indirect_access()`, that\n> ");

    private_function();
}

// my/nested.rs
pub fn function() {
    println!("called `my::nested::function()`");
}

#[allow(dead_code)]
fn private_function() {
    println!("called `my::nested::private_function()`");
}

// my/inaccessible.rs
#[allow(dead_code)]
pub fn public_function() {
    println!("called `my::inaccessible::public_function()`");
}

from:http://rustbyexample.com/mod/split.html


天赢金创
338 声望21 粉丝

天赢金创,天赢金创,天赢金创,天赢金创,天赢金创,天赢金创天赢金创,天赢金创


« 上一篇
NEJ 模板引擎
下一篇 »
Rust Borrowing