如何在 Rust 中匹配结构体字段? [英] How to match struct fields in Rust?
本文介绍了如何在 Rust 中匹配结构体字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Rust 可以匹配结构域吗?例如,这段代码:
Can Rust match struct fields? For example, this code:
struct Point {
x: bool,
y: bool,
}
let point = Point { x: false, y: true };
match point {
point.x => println!("x is true"),
point.y => println!("y is true"),
}
应该导致:
y is true
推荐答案
Rust 可以匹配结构体字段吗?
Can Rust match struct fields?
它在 "解构结构" 章节.
match point {
Point { x: true, .. } => println!("x is true"),
Point { y: true, .. } => println!("y is true"),
_ => println!("something else"),
}
这篇关于如何在 Rust 中匹配结构体字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文