$ rustc --version
rustc 1.44.0 (49cae5576 2020-06-01)
將代碼存在到不同的文件
main.rs
mod aa; fn main() { println!("------------------------------------"); aa::aa1(); }
mod aa表示引入aa模塊,在rust中一個文件名就代表一個模塊,創建aa.rs文件
aa.rs
pub fn aa1(){ println!("---aa-----"); }
$ cargo run Compiling crate_path v0.1.0 (/opt/wks/rust_study/crate_path) Finished dev [unoptimized + debuginfo] target(s) in 0.20s Running `target/debug/crate_path` ------------------------------------ ---aa-----
將代碼存到不到的目錄
src下第一層目錄文件結構
aa
aa.rs
main.rs
aa目錄下有文件
bb.rs
cc.rs
rust中mod模塊與文件名一一對應,存在一個mod aa,就必須得有一個叫aa.rs的文件存在;
但文件中不能再放文件,目錄才可以放文件,於是rust就創建一個同名的目錄,也叫aa,目錄aa下再放其他文件
main.rs
mod aa; fn main() { println!("------------------------------------------------"); aa::bb::bb1(); aa::cc::cc1(); }
aa.rs
pub mod bb;
pub mod cc;
aa/bb.rs
pub fn bb1(){ println!("-----------bb1-----------"); }
aa/cc.rs
pub fn cc1(){ println!("--------------cc1------------"); }
上面是兩個可運行的例子,下面是完整的體系式的概念
Packages and Crates
A package is one or more crates that provide a set of functionality. A package contains a Cargo.toml file that describes how to build those crates.
A crate is a binary or library.
src/main.rs is the crate root of a binary crate with the same name as the package.
Likewise, Cargo knows that if the package directory contains src/lib.rs, the package contains a library crate with the same name as the package, and src/lib.rs is its crate root. Cargo passes the crate root files to rustc
to build the library or binary.
$ cargo new my-project Created binary (application) `my-project` package $ ls my-project Cargo.toml src $ ls my-project/src main.rs
Here, we have a package that only contains src/main.rs, meaning it only contains a binary crate named my-project
. If a package contains src/main.rs and src/lib.rs, it has two crates: a library and a binary, both with the same name as the package. A package can have multiple binary crates by placing files in the src/bin directory: each file will be a separate binary crate.
Defining Modules to Control Scope and Privacy
the use
keyword that brings a path into scope; and the pub
keyword to make items public. We’ll also discuss the as
keyword, external packages, and the glob operator.
. Create a new library named restaurant
by running cargo new --lib restaurant
;
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
fn seat_at_table() {}
}
mod serving {
fn take_order() {}
fn serve_order() {}
fn take_payment() {}
}
}
fn main() {}
By using modules, we can group related definitions together and name why they’re related. Programmers using this code would have an easier time finding the definitions they wanted to use because they could navigate the code based on the groups rather than having to read through all the definitions. Programmers adding new functionality to this code would know where to place the code to keep the program organized.
Earlier, we mentioned that src/main.rs and src/lib.rs are called crate roots. The reason for their name is that the contents of either of these two files form a module named crate
at the root of the crate’s module structure, known as the module tree.
crate
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
└── take_payment
Paths for Referring to an Item in the Module Tree
A path can take two forms:
An absolute path starts from a crate root by using a crate name or a literal crate. A relative path starts from the current module and uses self, super, or an identifier in the current module. Both absolute and relative paths are followed by one or more identifiers separated by double colons (::).
Bringing Paths into Scope with the use Keyword
創建新項目
aa.rs
mod bb { pub mod bb1{ pub fn bb1_1(){ println!("aa --> bb --> bb1 --> bb1_1"); } pub fn bb1_2(){ println!("-----self ----------------"); self::bb1_1(); } } pub fn bb2(){ println!("aa --> bb --> bb2"); } } //相當於在本crate中提供一個pub方法,供外部調用 use self::bb::bb1::bb1_1 as b1; pub fn aa1(){ b1(); } //讓pub bb2在其父mod bb私有的提前下,可供外部程序調用 //bb2本身必須pub pub use self::bb::bb2; //對外提供一個pub模塊 pub mod cc { //crate是絕對路徑,root crate是main.rs use crate::aa::bb::bb1::bb1_2 as b2; pub fn cc1(){ b2(); } pub mod cc2{ pub fn cc2_1(){ println!("aa --> cc --> cc2 --> cc2_1"); } } }
main.rs
mod aa; use aa::cc::cc2; fn main(){ println!("-----------------------------------"); aa::aa1(); aa::cc::cc1(); aa::bb2(); cc2::cc2_1(); }
輸出
$ cargo run Compiling test_lib v0.1.0 (/opt/wks/rust_study/test_lib) Finished dev [unoptimized + debuginfo] target(s) in 0.21s Running `target/debug/test_lib` ----------------------------------- aa --> bb --> bb1 --> bb1_1 -----self ---------------- aa --> bb --> bb1 --> bb1_1 aa --> bb --> bb2 aa --> cc --> cc2 --> cc2_1
======================================================================================
上面的方式是可以運行的,但每個目錄還要建立一個同名的rs文件,看上去不怎么好看。下面介紹另外一種方式
將同名的rs文件放到同名的目錄下,統一重命名為mod.rs
其他的沒有變化,移動位置,重命名為mod.rs,然后就可以了