パッケージ・クレート・モジュール
理解
1 つの package には library crate を最大 1 つだけ含められますが、binary crate は複数含められます。
module と pub
mod front_of_house { // 其他 module 引用这里的嵌套 module,需要有 pub 修饰 pub mod hosting { // 其他 module 引用这里的 fn 也需要有 pub 修饰 pub fn add_to_waitlist() {} } // 引用嵌套 struct 也需要有 pub 修饰 pub struct Breakfast { // struct 内的 field 默认都是 private,外部访问也需要 pub 修饰 pub toast: String, seasonal_fruit: String, } impl Breakfast { pub fn summer(toast: &str) -> Breakfast { Breakfast { toast: String::from(toast), seasonal_fruit: String::from("peaches"), } } } } pub fn eat_at_restaurant() { // Absolute path crate::front_of_house::hosting::add_to_waitlist(); // Relative path front_of_house::hosting::add_to_waitlist(); }module と use
// 类似于 C++ 的 using namespace mod front_of_house { pub mod hosting { pub fn add_to_waitlist() {} } } use crate::front_of_house::hosting; pub fn eat_at_restaurant() { hosting::add_to_waitlist(); hosting::add_to_waitlist(); hosting::add_to_waitlist(); } // 支持 use 的时候用 pub 修饰,也称 Re-exporting,外部使用时可直接 hosting::xxx pub use crate::front_of_house::hosting; // 可以简化 use use std::cmp::Ordering; use std::io; use std::{cmp::Ordering, io}; // 简化时可以用 self use std::io; use std::io::Write; use std::io::{self, Write}; // 可以用 * 引入所有 module use std::collections::*;
mod a; と書くと、同じディレクトリにある a フォルダーまたはファイルが読み込まれます。
参照元
…
package は一連の機を提供する 1 つ以上の crate です。package には、その crate のビルド方法を記述した Cargo.toml ファイルが含まれます。
package に含められる容にはいくつかのルールがあります。package に含められる library crate は最大 1 つです。binary crate は好きな数だけ含められますが、少なくとも 1 つの crate(library または binary)が必要です。
…
モジュールを別々のファイルに分ける
ここまで、この章の例では複数のモジュールを 1 つのファイルに定義してきました。モジュールが大きくなったら、コードを見つけやすくするため、定義を別ファイルへ移したくなるでしょう。
For example, let’s start from the code in Listing 7-17 and move the front_of_house module to its own file src/front_of_house.rs by changing the crate root file so it contains the code shown in Listing 7-21. In this case, the crate root file is src/lib.rs, but this procedure also works with binary crates whose crate root file is src/main.rs.
ファイル名: src/lib.rs
mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
Listing 7-21: Declaring the front_of_house module whose body will be in src/front_of_house.rs
And src/front_of_house.rs gets the definitions from the body of the front_of_house module, as shown in Listing 7-22.
ファイル名: src/front_of_house.rs
pub mod hosting {
pub fn add_to_waitlist() {}
}
Listing 7-22: Definitions inside the front_of_house module in src/front_of_house.rs
Using a semicolon after mod front_of_house rather than using a block tells Rust to load the contents of the module from another file with the same name as the module. To continue with our example and extract the hosting module to its own file as well, we change src/front_of_house.rs to contain only the declaration of the hosting module:
ファイル名: src/front_of_house.rs
pub mod hosting;
Then we create a src/front_of_house directory and a file src/front_of_house/hosting.rs to contain the definitions made in the hosting module:
ファイル名: src/front_of_house/hosting.rs
pub fn add_to_waitlist() {}
モジュールツリーは変わらず、定義が別ファイルにあっても eat_at_restaurant の関数呼び出しは変なしで動作します。この方法なら、モジュールが大きくなったときに新しいファイルへ移せます。
src/lib.rs の pub use crate::front_of_house::hosting 文も変わっていません。また、use は crate の一部としてコンパイルされるファイルに影響しません。mod キーワードがモジュールを宣言すると、Rust はモジュールと同じ名前のファイルから、そのモジュールに入るコードを読み込みます。