Skip to main content

運算式 (Expression)

在程式語言中,運算式是會回傳值的程式碼單位。在 Move 中,除了作為宣告的 let 陳述式以外,幾乎所有內容都是運算式。本節將介紹各類運算式,並引入作用域的概念。

運算式以分號 ; 串接。若分號後方「沒有運算式」,編譯器會插入一個 unit ()——代表空運算式的值。

字面值 (Literals)​

在 基本型別 章節中,我們介紹了 Move 的基本型別,並使用字面值加以說明。字面值是用來在原始碼中表示固定值的表示法。字面值可用於初始化變數,或直接將固定值作為引數傳遞給函式。Move 支援下列字面值:

  • 布林值:true 與 false
  • 整數值:0、1、123123
  • 十六進位值:以 0x 為前綴、用於表示整數的數字,例如 0x0、0x1、0x123
  • 位元組向量值:以 b 為前綴,例如 b"bytes_vector"
  • 位元組值:以 x 為前綴的十六進位字面值,例如 x"0A"
  • 字串值:以雙引號包覆的文字,例如 "hello"。與其他字面值不同,字串字面值的型別會由情境推斷——它可以是 vector<u8>,或兩種標準字串型別之一。字串會在 字串 章節中詳細說明。
let b = true; // true is a literal
let n = 1000; // 1000 is a literal
let h = 0x0A; // 0x0A is a literal
let v = b"hello"; // b"hello" is a byte vector literal
let x = x"0A"; // x"0A" is a byte vector literal
let c = vector[1, 2, 3]; // vector[] is a vector literal
let s: std::string::String = "hello"; // "hello" is a string literal

運算子 (Operators)​

算術、邏輯與位元運算子用於對值執行運算。由於這些運算會產生值,因此被視為運算式。整數運算子及其會在何時中止,列於 基本型別 章節中。

let sum = 1 + 2; // 1 + 2 is an expression
let sum = (1 + 2); // the same expression with parentheses
let is_true = true && false; // true && false is an expression
let is_true = (true && false); // the same expression with parentheses

區塊 (Blocks)​

區塊是由大括號 {} 包圍的一連串陳述式與運算式。它會回傳區塊中最後一個運算式的值(請注意,這個最後的運算式不可有結尾分號)。區塊本身就是運算式,因此可用於任何預期使用運算式的位置。

// block with an empty expression, however, the compiler will
// insert an empty expression automatically: `let none = { () }`
// let none = {};

// block with let statements and an expression.
let sum = {
let a = 1;
let b = 2;
a + b // last expression is the value of the block
};

// block is an expression, so it can be used in an expression and
// doesn't have to be assigned to a variable.
{
let a = 1;
let b = 2;
a + b; // not returned - semicolon.
// compiler automatically inserts an empty expression `()`
};

區塊也會界定 作用域:在區塊內宣告的變數只會存在到區塊的右大括號為止。當值的作用域結束時究竟會發生什麼,是 Move 中的重要問題;所有權與作用域 章節專門說明此議題。

函式呼叫 (Function Calls)​

下一節的 函式 會詳細介紹函式。此處只需知道函式呼叫是一種運算式:它會呼叫函式,並回傳函式本體中最後一個運算式的值,前提是最後一個運算式沒有結尾分號。

fun add(a: u8, b: u8): u8 {
a + b
}

#[test]
fun some_other() {
let sum = add(1, 2); // not returned due to the semicolon.
// compiler automatically inserts an empty expression `()` as return value of the block
}

控制流程運算式 (Control Flow Expressions)​

控制流程運算式用於控制程式的流程。它們也是運算式,因此會回傳值。我們會在 控制流程 章節中介紹控制流程運算式。以下是非常簡要的概覽:

// if is an expression, so it returns a value; if there are 2 branches,
// the types of the branches must match.
if (bool_expr) expr1 else expr2;

// while is an expression, but it returns `()`.
while (bool_expr) { expr; };

// loop is an expression, but returns `()` as well.
loop { expr; break };

延伸閱讀 (Further Reading)​