2.3 解构结构体

03-解构结构体 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/pattern-matching/destructuring-structs.html

2.3 解构结构体

与元组一样,结构体也可以通过匹配来解构:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0
struct Move {
    delta: (i32, i32),
    repeat: u32,
}

#[rustfmt::skip]
fn main() {
    let m = Move { delta: (10, 0), repeat: 5 };

    match m {
        Move { delta: (0, 0), .. }        => println!("Standing still"),
        Move { delta: (x, 0), repeat }    => println!("{repeat} step x: {x}"),
        Move { delta: (0, y), repeat: 1 } => println!("Single step y: {y}"),
        _                                 => println!("Other move"),
    }
}
  • 修改 m 中的字面量,使它匹配其他模式。
  • 给 Movement 增加新字段,并按需要改模式。
  • 注意 delta: (x, 0) 是嵌套模式。

深入探索

  • 试一下 match &m,并查看捕获的类型。模式语法不变,但捕获会变成共享引用。这就是 match ergonomics, 在为枚举实现方法时写 match self 常常很有用。
    • 对 match &mut m 同样成立:捕获会变成独占引用。
  • 捕获与常量表达式的区别有时很难一眼看出。试着把第一个分支里的 10 改成变量,会发现它微妙地不工作;改成 const 后又会正常。
最后修改 August 11, 2026: 更新 (70a5af133)