mirror of https://github.com/sunface/rust-course
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.1 KiB
81 lines
1.1 KiB
3 years ago
|
# C-表达式
|
||
|
|
||
|
在[语句与表达式]章节中,我们对表达式有过介绍,下面对这些常用表达式进行一一说明。
|
||
|
|
||
|
### 基本表达式
|
||
|
```rust
|
||
|
let n = 3;
|
||
|
let s = "test";
|
||
|
```
|
||
|
|
||
|
### if表达式
|
||
|
```rust
|
||
|
fn main() {
|
||
|
let var1 = 10;
|
||
|
|
||
|
let var2 = if var1 >= 10 {
|
||
|
var1
|
||
|
} else {
|
||
|
var1 + 10
|
||
|
};
|
||
|
|
||
|
println!("{}", var2);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
通过`if`表达式将值赋予`var2`.
|
||
|
|
||
|
你还可以在循环中结合`continue`、`break`来使用:`
|
||
|
```rust
|
||
|
let mut v = 0;
|
||
|
for i in 1..10 {
|
||
3 years ago
|
v = if i == 9 {
|
||
|
continue
|
||
|
} else {
|
||
|
i
|
||
|
}
|
||
3 years ago
|
}
|
||
|
println!("{}",v);
|
||
|
```
|
||
|
|
||
|
### if let表达式
|
||
|
```rust
|
||
|
let o = Some(3);
|
||
|
let v = if let Some(x) = o {
|
||
|
x
|
||
|
} else {
|
||
|
0
|
||
|
};
|
||
|
```
|
||
|
|
||
|
### match表达式
|
||
|
```rust
|
||
|
let o = Some(3);
|
||
|
let v = match o {
|
||
|
Some(x) => x,
|
||
|
_ => 0
|
||
|
};
|
||
|
```
|
||
|
|
||
|
### loop表达式
|
||
|
```rust
|
||
|
let mut n = 0;
|
||
|
let v = loop {
|
||
|
if n == 10 {
|
||
|
break n
|
||
|
}
|
||
|
n += 1;
|
||
|
};
|
||
|
```
|
||
|
|
||
|
### 语句块{}
|
||
|
```rust
|
||
|
let mut n = 0;
|
||
|
let v = {
|
||
|
println!("before: {}", n);
|
||
|
n += 1;
|
||
|
println!("after: {}", n);
|
||
|
n
|
||
|
};
|
||
|
println!("{}", v);
|
||
|
```
|