E0027

未匹配的标注

结构的模式并未为结构的每个字段指定子模式。

错误的代码示例:

struct Dog {
    name: String,
    age: u32,
}

let d = Dog { name: "Rusty".to_string(), age: 8 };

// This is incorrect.
match d {
    Dog { age: x } => {}
}

要修复此错误,请确保结构定义中的每个字段都在模式中提到,或使用 .. 忽略剩余字段。例如:

struct Dog {
    name: String,
    age: u32,
}

let d = Dog { name: "Rusty".to_string(), age: 8 };

match d {
    Dog { name: ref n, age: x } => {}
}

// 这也是对的(忽略未使用字段)
match d {
    Dog { age: x, .. } => {}
}

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/rust-rcei-2020/...

译文地址:https://learnku.com/docs/rust-rcei-2020/...

上一篇 下一篇
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~