是否可以在多种类型的Rust生成模式匹配?看起来好像你不能。如果没有,是否有计划的支持来添加它或运行时类型信息(RTTI)?与多种类型匹配的生锈模式
struct Bus;
struct Car;
struct Person;
fn main() {
let x = Bus;
//or more realistically, let x = function_with_multiple_return_types();
match x {
Car => {
// ...
}
Bus => {
// this gets executed
}
Person => {
// ...
}
}
}
这个例子是微不足道的。在现实生活中,只有当x
可以是多种类型时才有用。例如let x = function_with_multiple_return_types();
。正如Shepmaster所说,这是不可能的,因为Rust是静态类型的,与Lisp不同。
Idk Rust,但我希望匹配的每个子句都需要是相同的类型。尽量让它们成为同一个超类/界面的一部分 – Carcigenicate
您可以得到的最接近的东西可能是标记联合,如示例[here](https://doc.rust-lang.org/book/enums.html)。 – squiguy