Skip to main content

模組 (Module)

模組是 Move 中程式碼組織的基本單位。模組用於將程式碼分組並隔離,模組的所有成員預設對模組外部是私有的。這使得模組成為一個信任邊界:如後續章節所示,只有定義某型別的模組才能建立、修改與銷毀該型別的值。在本節中,你將學習如何定義模組、宣告其成員,以及如何從其他模組存取它。

模組宣告 (Module Declaration)

模組使用 module 關鍵字宣告,後面接套件地址與模組名稱,兩者以 :: 分隔,接著是分號與模組主體。模組名稱應使用 snake_case 風格——全部小寫字母,單詞之間以底線分隔。模組名稱在套件中必須是唯一的。

通常,sources/ 資料夾中的一個檔案包含一個模組。檔案名稱應與模組名稱一致——例如,donut_shop 模組應存放在 donut_shop.move 檔案中。你可以在 程式碼慣例 章節中閱讀更多關於程式碼慣例的內容。

如果你需要在一個檔案中宣告多個模組,你必須使用 模組區塊 (Module Block) 語法。

// Module label.
module book::my_module;

// module body

地址與具名地址 (Address and Named Address)

模組地址可以用兩種方式指定:作為地址 字面量(不需要 @ 前綴),或作為 套件清單 (Package Manifest) 中宣告的套件名稱。

module 0x0::address_literal { /* ... */ }
module book::named_address { /* ... */ }

Move.toml 中的 Package 區段:

[package]
name = "book"
edition = "2024"

模組成員 (Module Members)

模組成員在模組主體內宣告。為了說明這點,讓我們定義一個簡單的模組,其中包含一個匯入、一個常數、一個結構體與一個函式:

module book::my_module_with_members;

// import - brings the `my_module` module into scope
use book::my_module;

// a constant - an immutable, module-private value
const CONST: u8 = 0;

// a struct - a custom data type
public struct Struct {}

// a function - a unit of executable code
fun function() { /* function body */ }

每個成員都以自己的關鍵字開頭:use 將其他模組帶入作用域 (匯入模組)、const 定義一個永遠不變的值 (常數)、struct 宣告一個自訂資料型別(結構體),而 fun 宣告一個函式(函式)。目前先不用擔心細節——這些內容在本章中各自都有專門的章節;現在只需要認識這些關鍵字,並知道它們都存在於模組層級即可。

模組區塊 (Module Block)

Move 的 2024 版之前的版本要求模組主體必須是一個 模組區塊——即模組的內容以大括號 {} 包裹。這種區塊語法仍然受到支援,而選擇它而非上述 標籤 語法的唯一理由,是在一個檔案中宣告多個模組——這種情況很少需要,也不是建議的做法。

module book::my_block_module_with_members {
// import
use book::my_module;

// a constant
const CONST: u8 = 0;

// a struct
public struct Struct {}

// method alias
public use fun function as Struct.struct_fun;

// function
fun function(_: &Struct) { /* function body */ }
}

// module block allows multiple module definitions in the
// same file but this is not a recommended practice
module book::another_module_in_the_file {
// ...
}

延伸閱讀 (Further Reading)