03-语句与表达式
语句与表达式 — Rust By Practice
内容来源 · Rust By Practice / Rust 语言实战
原文链接: https://practice-rust-zh.beatai.org/basic-types/statements-expressions.html
语句与表达式
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| fn main() {
let x = 5u32;
let y = {
let x_squared = x * x;
let x_cube = x_squared * x;
// 下面表达式的值将被赋给 `y`
x_cube + x_squared + x
};
let z = {
// 分号让表达式变成了语句,因此返回的不再是表达式 `2 * x` 的值,而是语句的值 `()`
2 * x;
};
println!("x is {:?}", x);
println!("y is {:?}", y);
println!("z is {:?}", z);
}
|
练习
- 🌟🌟
1
2
3
4
5
6
7
8
9
10
| // 使用两种方法让代码工作起来
fn main() {
let v = {
let mut x = 1;
x += 2
};
// assert_eq!(v, 3);
assert_eq!(v, ());
}
|
- 🌟
1
2
3
4
5
6
7
8
9
10
|
fn main() {
// let v = (let x = 3);
let v = {
let x = 3;
x
};
assert!(v == 3);
}
|
- 🌟
1
2
3
4
5
6
7
8
9
10
|
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
}
fn sum(x: i32, y: i32) -> i32 {
// x + y;
x + y
}
|
你可以在这里找到答案(在 solutions 路径下)