I have a boxed tuple to avoid recursion. However, when I pattern match against the tuple, I can't seem to get at both tuple values. To illustrate my problem, take 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));
}
I get this error:
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
I've read about boxed syntax, but I'd like to avoid unstable features if at all possible. It feels like the only answer is redefining Branch
as
Branch(Box<Foo>, Box<Foo>)
but this seems like it avoids answering the question (which is admittedly mostly a thought exercise at this point).