【发布时间】:2017-03-25 03:03:53
【问题描述】:
好像你做不到。如果没有,是否有计划支持添加它或运行时类型信息 (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();。
【问题讨论】:
-
Idk Rust,但我希望匹配的每个子句都需要是相同的类型。尝试使它们都成为同一个超类/接口的一部分
-
你能得到的最接近的可能是一个带标签的联合,例如示例here。
标签: pattern-matching rust