Skip to main content

註解 (Comments)

註解可用來新增備註或記錄你的程式碼。編譯器會忽略註解,且不會產生 Move 位元組碼。你可以使用註解說明程式碼的作用、為自己或其他開發者新增備註、暫時移除部分程式碼,或產生文件。Move 中有三種註解:單行註解、區塊註解與文件註解。

單行註解 (Line Comment)​

你可以使用雙斜線 // 註解該行其餘內容。// 之後的所有內容都會被編譯器忽略。

module book::comments_line;

// let's add a note to everything!
fun some_function_with_numbers() {
let a = 10u8;
// let b = 10 this line is commented and won't be executed
let b = 5; // here comment is placed after code
a + b; // result is 15, not 10!
}

區塊註解 (Block Comment)​

區塊註解用於註解一個程式碼區塊。它們以 /* 開始,並以 */ 結束。/* 與 */ 之間的所有內容都會被編譯器忽略。你可以使用區塊註解註解單行或多行內容,甚至可以用它們註解一行中的部分內容。

module book::comments_block;

fun /* you can comment everywhere */ go_wild() {
/* here
there
everywhere */ let a = 10;
let b = /* even here */ 10; /* and again */
a + b;
}
/* you can use it to remove certain expressions or definitions
fun empty_commented_out() {

}
*/

這個範例有些極端,但它展示了使用區塊註解的所有方式。

文件註解 (Doc Comment)​

文件註解是用來為你的程式碼產生文件的特殊註解。它們類似單行註解,但以三個斜線 /// 開始,並放置於所記錄項目的定義之前,例如模組、結構、函式或常數。

/// Module has documentation!
module book::comments_doc;

/// This is a 0x0 address constant!
const AN_ADDRESS: address = @0x0;

/// This is a struct!
public struct AStruct {
/// This is a field of a struct!
a_field: u8,
}

/// This function does something!
/// And it's documented!
fun do_something() {}

文件工具會將公開成員的文件註解收集到參考頁面中;本書各處連結的標準函式庫與框架文件正是以這種方式產生。撰寫良好的文件註解會說明函式的作用,以及它會在何種條件下中止。

空白字元 (Whitespace)​

與某些語言不同,空白字元(空格、定位字元與換行)不會影響程式的含義。