本文內容
這篇文章是實戰性質的,也就是說原理部分較少,屬于經驗總結,rust對于模塊的例子太少了,rust特性比較多(悲),本文的內容可能只是一部分,實作方式也不一定是這一種,
關于 rust 模塊的相關內容,準確來說:怎么在原始碼中參考其他模塊的內容,
- 關于
mod、use、as這幾個關鍵字(檔案名) - 關于
mod.rs檔案 - 關于
self、super、crate這幾個路徑關鍵字 worksapce:本文不討論,狹義上指的是cargo的[workspace]部分: 可參見 The [workspace] sectionpackage: 狹義上指的是cargo的[package]部分,參見 The [package] sectioncrate:參見下文 關于 create 的定義和 cargo 管理下的 cratemodule:本文的重點, crate 里 會有多個 module,文本討論重點就是 mod 之間相互參考的問題,
一、mod 關鍵字和 mod.rs 檔案,其他普通檔案 foo.rs 等
參考模塊要搞清楚的:
- 是在哪里參考的,也就要參考的檔案的位置
- 需要參考那個模塊,跟這個檔案的相對位置和絕對位置是什么
mod 關鍵字:
- 用來宣告,表現方式是包裹一個代碼塊,也就是說這個代碼塊會成為一個單獨的模塊,
mod xxx { <rust陳述句塊> }:內部寫 rust 陳述句 ,見 例一#[path="...xxxx.rs"] mod xxx;:使用 path 屬性 ,使用見例二mod xxx { include!("...xxxx.rs") }:內部配合include!宏,使用見例二
- 用來宣告(“參考”)其他“模塊”,(一個檔案隱含的表示為一個mod)
假設使用mod toy;陳述句來引入一個模塊,實際上這跟你在哪里寫的這個陳述句有關系有關- 把當前的檔案所在位置分為兩類, 型別A:位置在
src/main.rs、src/lib.rs或者xxx/.../mod.rs位置上的檔案; 型別B: 位置不在1中的檔案, - 在位置型別 A 的檔案使用代碼
mod toy;,你實際上在告訴編譯器,你需要的模塊是與此檔案同級的toy.rs或者toy/mod.rs檔案 ,編譯器會自己找下這兩個被參考的位置,如果兩個位置都有檔案,則報錯,見 例三 - 在位置型別 B 的檔案使用代碼
mod toy;,你實際上在告訴編譯器,你需要的模塊是與此檔案(假設檔案名為foo.rs)同級的foo檔案夾下的foo/toy.rs或者foo/toy/mod.rs檔案,編譯器會自己找下這兩個被參考的位置,如果兩個位置都有檔案,則報錯, 見 例四
- 把當前的檔案所在位置分為兩類, 型別A:位置在
二、使用 use 和 as 關鍵字縮短匯入陳述句,或者匯出模塊內容
use 關鍵字也有兩個作用
- 縮短陳述句,使用 use 為當前檔案縮短長陳述句,減少重復性的代碼,見 例子五
- 配合
as關鍵字一起使用,進一步減少重復代碼,或者防止名字重復,或者取個順眼的名字 例子六 - 打包其他模塊的內容,當做本模塊的內容一起匯出,見 例子七
三、使用 super 和 crate 進行相對路徑和絕對路徑(頂級路徑)的訪問
如果我們想要呼叫一個函式,我們需要知道它的路徑,路徑有兩種形式:
- 絕對路徑(absolute path)從 crate 根部開始,以 crate 名或者字面量 crate 開頭, 見例八
- 相對路徑(relative path)從當前模塊開始,以 self、super 或當前模塊的識別符號開頭,
見檔案: https://rustwiki.org/zh-CN/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html
四、關于 create 的定義和 cargo 管理下的 crate
我們都知道通過 cargo 創建出的工程中 src/main.rs 就是程式的入口,但是還有更多的使用方式,
- rustc 命令 : rust編譯器,就算沒有cargo也可以生成程式,但是比較麻煩,這些都讓cargo來處理就好
- cargo 命令 : 專案管理工具
下面就是一些問題了
- 什么是 create ? rustc 的編譯入口檔案,這個檔案就被當做 crate 檔案,
- crate 型別: 有多種,最常見的是
bin和lib,其他型別參見 rust參考手冊-鏈接 - cargo 怎么定義工程專案中哪些是需要編譯的 crate 的? 參見: cargo手冊-專案布局
? src/ # 包含源檔案的目錄
lib.rs # 庫和包的主要入口點
main.rs # 包生成可執行檔案的主要入口點
? bin/ # (可選)包含其他可執行檔案的目錄
*.rs
? */ # (可選)包含多檔案可執行檔案的目錄
main.rs
? examples/ # (可選)示例
*.rs
? */ # (可選)包含多檔案示例的目錄
main.rs
? tests/ # (可選)集成測驗
*.rs
? */ # (可選)包含多檔案測驗的目錄
main.rs
? benches/ # (可選)基準
*.rs
? */ # (可選)包含多檔案基準的目錄
main.rs
五、例子
例一:單檔案,主函式和 toy 模塊
- 注意
run函式需要加pub關鍵字,否則不會被匯出
src/main.rs
mod toy {
pub fn run() {
println!("run toy");
}
}
fn main() {
toy::run();
}
輸出
run toy
例二:兩個檔案,主函式和另一個檔案夾 toy 模塊
src/toy_implements.rs
pub fn run() {
println!("run toy_impl !");
}
src/main.rs
mod toy1 { // 方法1: 使用 include!
include!("./toy_implements.rs");
}
#[path ="./toy_implements.rs"]
mod toy2; // 方法2: 使用 path 屬性定位檔案位置
fn main() {
toy1::run();
toy2::run();
}
輸出
run toy_impl !
run toy_impl !
例三:在 main.rs 中使用 mod toy;
src/toy.rs
pub fn run() {
println!("run toy_impl !");
}
src/main.rs
mod toy;
fn main() {
toy::run();
}
輸出
run toy_impl !
例四:在 src/foo.rs 中使用 mod toy;
src/foo/toy.rs
pub fn run() {
println!("run toy_impl !");
}
src/foo.rs
mod toy;
fn say_hi() {
toy::run();
}
輸出
run toy_impl !
例五:use 指令
之前,我們使用了 toy::run() 來呼叫 run 函式,現在,我們使用 use 關鍵字來匯入 toy 模塊里的內容,這樣就能在 main 函式中直接使用
src/foo.rs
mod toy {
pub fn run() { // 注意使用 pub 關鍵字
println!("run toy");
}
}
fn main() {
use toy::*; // 使用 use 匯入 toy 模塊里的內容
run(); // 直接呼叫
}
例六: 在as配合use指令
src/foo.rs
mod toy {
pub fn run() { // 注意使用 pub 關鍵字
println!("run toy");
}
}
fn main() {
use toy::run as toy_run; // 使用 use + as 匯入 toy 模塊里的內容
toy_run();
}
例七: 使用pub use命令在mod.rs合并打包其他模塊的東西
src/toy/runner.rs
pub fn dog_run() { println!("dog is run !"); }
src/toy/fly.rs
pub fn fly_bird() { println!("bird is fly !"); }
src/toy/bear.rs
pub fn bear_eat() { println!("bear is eat fish !"); }
pub fn bear_sleep() { println!("bear is go sleep !"); }
src/toy/mod.rs
mod runner; // 引入同級 runner.rs 檔案
mod fly; // 引入同級 fly.rs 檔案
mod bear; // 引入同級 bear.rs 檔案
pub use runner::dog_run; // 宣告(匯出) dog_run 函式
pub use fly::fly_bird as now_fly_brid; // 宣告(匯出) fly_bird 函式,并重命名為 now_fly_brid
pub use bear::*; // 宣告(匯出) dog_run 函式
src/main.rs
mod toy;
fn main() {
toy::dog_run();
toy::now_fly_brid();
toy::bear_eat();
toy::bear_sleep();
}
輸出
dog is run !
bird is fly !
bear is eat fish !
bear is go sleep !
例七: 使用pub mod匯出內部包,使用 crate 參考頂部內容
src/toy/cube/mod.rs
pub fn get_size() {
println!("size is in main");
crate::top_size(); // 必不可少的 crate 關鍵字
}
src/toy/mod.rs
pub mod cube;
src/main.rs
mod toy;
fn top_size() {
println!("top size one !")
}
fn main() {
toy::cube::get_size();
}
輸出
size is in main
top size one !
參考文獻
!!強烈推薦看下面的參考做補充!!
- 模塊的官方參考: https://rustwiki.org/zh-CN/reference/items/modules.html
- Rust 程式設計語言(7. 使用包、Crate和模塊管理不斷增長的專案): https://rustwiki.org/zh-CN/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html
- Rust 模塊和檔案(譯文): https://zhuanlan.zhihu.com/p/73544030
- Rust 模塊和檔案(原文): https://amos.me/blog/2019/rust-modules-vs-files/
- crate: https://rustwiki.org/zh-CN/rust-by-example/crates.html
- crate 型別: https://rustwiki.org/zh-CN/reference/linkage.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543652.html
標籤:其他
上一篇:搜書吧賬號自動登錄功能
下一篇:fusion app登錄注冊示例
