udpate 解构式赋值

pull/473/merge
sunface 3 years ago
parent 2771cbb714
commit 7364e2fff6

@ -92,6 +92,7 @@
- [Unsafe Rust](advance/unsafe/intro.md)
- [五种兵器](advance/unsafe/superpowers.md)
- [内联汇编 todo](advance/unsafe/inline-asm.md)
- [Macro宏编程](advance/macro.md)
<!-- - [SIMD todo](advance/simd.md) -->
<!-- - [高阶特征约束(HRTB) todo](advance/hrtb.md) -->

@ -115,13 +115,22 @@ fn main() {
### 解构式赋值
在 [Rust 1.59](https://course.rs/appendix/rust-versions/1.59.html) 版本后,我们可以在赋值语句的左式中使用元组、切片和结构体模式了。
```rust
let (a, b, c, d, e);
struct Struct {
e: i32
}
fn main() {
let (x, y) = (1.0, 2.0);
let (a, b, c, d, e);
(a, b) = (1, 2);
[c, .., d, _] = [1, 2, 3, 4, 5];
Struct { e, .. } = Struct { e: 5, f: 3 };
(a, b) = (1, 2);
[c, .., d, _] = [1, 2, 3, 4, 5];
Struct { e, .. } = Struct { e: 5 };
assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]);
assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]);
}
```
这种使用方式跟之前的 `let` 保持了一致性,但是 `let` 会重新绑定,而这里仅仅是对之前绑定的变量进行再赋值。

Loading…
Cancel
Save