Package 和 Crate
+++这一章很抽象,一定要自己多写,不然很难理解。
+Package 和 Crate
crate!我第一眼看成了 create…
@@ -271,6 +274,138 @@
crate
(箱) 是 Rust 的最小编译单元,一个package
中包含一个活多个crate
,crate
可以是一个二进制项目,或者一个库。使用 use 导入模块
可以使用
+use
关键字来简化导入:+// hosting.rs +use crate::product::provider; + +pub fn add_to_waitlist() {} + +pub fn seat_at_table() -> String { + provider::give() +} +
+
use
也可以直接导入函数:+// hosting.rs +use crate::product::provider::give; + +pub fn add_to_waitlist() {} + +pub fn seat_at_table() -> String { + give() +} +
限制可见性语法
+
+- pub 意味着可见性无任何限制
+- pub(crate) 表示在当前包可见
+- pub(self) 在当前模块可见
+- pub(super) 在父模块可见
+- pub(in
+) 表示在某个路径代表的模块中可见,其中 path 必须是父模块或者祖先模块 // 一个名为 `my_mod` 的模块 +mod my_mod { + // 模块中的项默认具有私有的可见性 + fn private_function() { + println!("called `my_mod::private_function()`"); + } + + // 使用 `pub` 修饰语来改变默认可见性。 + pub fn function() { + println!("called `my_mod::function()`"); + } + + // 在同一模块中,项可以访问其它项,即使它是私有的。 + pub fn indirect_access() { + print!("called `my_mod::indirect_access()`, that\n> "); + private_function(); + } + + // 模块也可以嵌套 + pub mod nested { + pub fn function() { + println!("called `my_mod::nested::function()`"); + } + + #[allow(dead_code)] + fn private_function() { + println!("called `my_mod::nested::private_function()`"); + } + + // 使用 `pub(in path)` 语法定义的函数只在给定的路径中可见。 + // `path` 必须是父模块(parent module)或祖先模块(ancestor module) + pub(in crate::my_mod) fn public_function_in_my_mod() { + print!("called `my_mod::nested::public_function_in_my_mod()`, that\n > "); + public_function_in_nested() + } + + // 使用 `pub(self)` 语法定义的函数则只在当前模块中可见。 + pub(self) fn public_function_in_nested() { + println!("called `my_mod::nested::public_function_in_nested"); + } + + // 使用 `pub(super)` 语法定义的函数只在父模块中可见。 + pub(super) fn public_function_in_super_mod() { + println!("called my_mod::nested::public_function_in_super_mod"); + } + } + + pub fn call_public_function_in_my_mod() { + print!("called `my_mod::call_public_funcion_in_my_mod()`, that\n> "); + nested::public_function_in_my_mod(); + print!("> "); + nested::public_function_in_super_mod(); + } + + // `pub(crate)` 使得函数只在当前包中可见 + pub(crate) fn public_function_in_crate() { + println!("called `my_mod::public_function_in_crate()"); + } + + // 嵌套模块的可见性遵循相同的规则 + mod private_nested { + #[allow(dead_code)] + pub fn function() { + println!("called `my_mod::private_nested::function()`"); + } + } +} + +fn function() { + println!("called `function()`"); +} + +fn main() { + // 模块机制消除了相同名字的项之间的歧义。 + function(); + my_mod::function(); + + // 公有项,包括嵌套模块内的,都可以在父模块外部访问。 + my_mod::indirect_access(); + my_mod::nested::function(); + my_mod::call_public_function_in_my_mod(); + + // pub(crate) 项可以在同一个 crate 中的任何地方访问 + my_mod::public_function_in_crate(); + + // pub(in path) 项只能在指定的模块中访问 + // 报错!函数 `public_function_in_my_mod` 是私有的 + //my_mod::nested::public_function_in_my_mod(); + // 试一试 ^ 取消该行的注释 + + // 模块的私有项不能直接访问,即便它是嵌套在公有模块内部的 + + // 报错!`private_function` 是私有的 + //my_mod::private_function(); + // 试一试 ^ 取消此行注释 + + // 报错!`private_function` 是私有的 + //my_mod::nested::private_function(); + // 试一试 ^ 取消此行的注释 + + // 报错! `private_nested` 是私有的 + //my_mod::private_nested::function(); + // 试一试 ^ 取消此行的注释 +} +