Rust模块(modules) Rust 提供了一套强大的模块(module)系统,可以将代码按层次分成多个逻辑 单元(模块),并管理这些模块之间的可见性(公有(public)或私有(private))。
模块是项(item)的集合,项可以是:函数,结构体,trait,impl
块,甚至其它模块。
可见性 默认情况下,模块中的项拥有私有的可见性(private visibility),不过可以加上 pub
修饰语来重载这一行为。模块中只有公有的(public)项可以从模块外的作用域 访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #[allow(non_snake_case)] #[allow(dead_code)] mod first{ fn firstPrivate (){ println! ("firstPrivate" ); } pub fn firstPublic (){ println! ("firstPublic" ); } pub mod second{ fn secondPrivate (){ println! ("secondPrivate" ); } pub fn secondPublic (){ println! ("secondPublic" ); } pub (self ) fn secondPublicSelf (){ println! ("secondPublicSelf" ); } pub (super ) fn secondPublicSuper (){ println! ("secondPublicSuper" ); } pub mod third{ pub (super ) fn thirdPublicSuper (){ println! ("thirdPublicSuper" ); } pub (in crate::first) fn thirdFunctionInFirst (){ println! ("thirdFunctionInFirst" ); } } pub fn function (){ secondPublicSelf(); third::thirdFunctionInFirst(); third::thirdPublicSuper(); } } pub fn function (){ firstPublic(); firstPrivate(); second::function(); second::secondPublic(); second::secondPublicSuper(); } } fn main (){ first::function(); }
运行结果如下:
1 2 3 4 5 6 7 firstPublic firstPrivate secondPublicSelf thirdFunctionInFirst thirdPublicSuper secondPublic secondPublicSuper
通过上面的例子可以看出一下几点:
模块是可以嵌套的。
模块中方法默认为私有,只能自己调用。
只声明pub为公有,任何地方都可以调用。
声明pub(self)为当前模块可见。
声明pub(super)为父模块可见(只有父模块)。
声明pub(in path)为指定路径向下可见(包括指定路径)。
结构体可见性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 fn main () { mod my{ pub struct OpenBox <T>{ pub contents : T } pub struct ClosedBox <T>{ contents :T } impl <T> ClosedBox<T> { pub fn new (contents: T) -> ClosedBox<T> { ClosedBox { contents: contents, } } } } let open_box = my::OpenBox{contents:"open_box" }; println! ("{}" ,open_box.contents); let closed_box = my::ClosedBox::new("close_box" ); }
use声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 fn main (){ use first::second::function as out_function; fn function (){ println! ("{}" ,"function" ); } mod first{ pub mod second{ pub fn function (){ println! ("{}" ,"first::second::function()" ); } } } out_function(); { use first::second::function; function(); } function(); }
super和self 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 fn function () { println! ("called `function()`" ); } mod cool { pub fn function () { println! ("called `cool::function()`" ); } } mod my { fn function () { println! ("called `my::function()`" ); } mod cool { pub fn function () { println! ("called `my::cool::function()`" ); } } pub fn indirect_call () { print! ("called `my::indirect_call()`, that\n> " ); self::function(); function(); self::cool::function(); cool::function(); super::function(); { use crate::cool::function as root_function; root_function(); } } } fn main () { my::indirect_call(); }
打印结果:
1 2 3 4 5 6 7 called `my::indirect_call()`, that > called `my::function ()` called `my::function()` called `my::cool::function()` called `my::cool::function()` called `function()` called `cool::function()`
模块文件分层 模块可以分配到文件/目录的层次结构中。
1 2 3 4 5 6 7 $ tree . . |-- my | |-- inaccessible.rs | |-- mod.rs | `-- nested.rs `-- main.rs
main.rs
的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mod my;fn function () { println! ("called `function()`" ); } fn main () { my::function(); function(); my::indirect_access(); my::nested::function(); }
my/mod.rs
的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 mod inaccessible;pub mod nested;pub fn function () { println! ("called `my::function()`" ); } fn private_function () { println! ("called `my::private_function()`" ); } pub fn indirect_access () { println! ("called `my::indirect_access()`" ); private_function(); }
my/nested.rs
的内容:
1 2 3 4 5 6 7 8 pub fn function () { println! ("called `my::nested::function()`" ); } #[allow(dead_code)] fn private_function () { println! ("called `my::nested::private_function()`" ); }
my/inaccessible.rs
的内容:
1 2 3 4 #[allow(dead_code)] pub fn public_function () { println! ("called `my::inaccessible::public_function()`" ); }
执行打印结果:
1 2 3 4 5 6 called `my::function()` called `function()` called `my::indirect_access()` called `my::private_function()` called `my::inaccessible::public_function()` called `my::nested::function()`