mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-08-16 00:06:16 +00:00
add destructuring assignments
This commit is contained in:
@@ -117,4 +117,22 @@ fn main() {
|
||||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 2);
|
||||
}
|
||||
```
|
||||
|
||||
### 解构式赋值
|
||||
🌟🌟 使用两种方式解决问题:
|
||||
|
||||
- 通过变量遮蔽: 添加 `let`
|
||||
- 令类型兼容
|
||||
|
||||
> Note: 解构式赋值只能在 Rust 1.59 或者更高版本中使用
|
||||
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let (mut x, mut y) = (1.0, 2.0);
|
||||
(x,y) = (3, 4);
|
||||
|
||||
assert_eq!([x,y],[3, 4]);
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user