<分区>
我有一个盒装元组来避免递归。但是,当我对元组进行模式匹配时,我似乎无法同时获得两个元组值。为了说明我的问题,取 the following code :
#[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>)
但这似乎避免了回答问题(目前公认这主要是一种思考练习)。