// 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. letsum = { leta = 1; letb = 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. { leta = 1; letb = 2; a + b; // not returned - semicolon. // compiler automatically inserts an empty expression `()` };
结构体
数据合集,用什么来表示长方形的长和宽。Move 不支持递归结构,这意味着结构不能将自身包含为字段。
1 2 3 4
public structArtist { name: String, age: u8, }
函数调用
add 的返回值是一个表达式 a+b,返回类型是u8
1 2 3 4 5 6 7 8
fun add(a: u8, b: u8): u8 { a + b }
#[test] fun some_other() { letsum = add(1, 2); // add(1, 2) is an expression with type u8 }
流程控制
它们也是表达式,因此它们返回一个值。
1 2 3 4 5 6 7 8 9
// 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 };