我有一个装箱的元组来避免递归。然而,当我对元组进行模式匹配时,我似乎无法获得两个元组值。为了说明我的问题,看一下以下代码:
#[derive(Clone, PartialEq, Debug)]
enum Foo {
Base,
Branch(Box<(Foo, Foo)>),
}
fn do_something(f: Foo) -> Foo {
match f {
Foo::Base => Foo::Base,
Foo::Branch(pair) => {
let (f1, f2) = *pair;
if f2 == Foo::Base {
f1
} else {
f2
}
}
}
}
fn main() {
let f = Foo::Branch(Box::new((Foo::Base, Foo::Base)));
println!("{:?}", do_something(f));
}
我遇到了这个错误:
error[E0382]: use of moved value: `pair`
--> src/main.rs:11:22
|
11 | let (f1, f2) = *pair;
| -- ^^ value used here after move
| |
| value moved here
|
= note: move occurs because `pair.0` has type `Foo`, which does not implement the `Copy` trait
我已经了解了关于包装语法的知识,但是如果可能的话,我想避免不稳定的功能。感觉唯一的答案就是将Branch
重新定义为
Branch(Box<Foo>, Box<Foo>)
但是这似乎避免回答问题(尽管现在它主要是一个思想实验)。