How can I pattern match a tuple containing a &mut enum?

Multi tool use
Multi tool use


How can I pattern match a tuple containing a &mut enum?



How can the code below be made to compile? It seems perfectly safe, but can't convince the compiler that it is.



The version matching *self gives the error: cannot move out of borrowed content on the line of the match


*self


cannot move out of borrowed content



The version matching self gives: use of moved value: *self


self


use of moved value: *self


enum Foo {
Foo1(u32),
Foo2(i16),
}

impl Foo {
fn bar(&mut self, y: u32) -> (u32, &mut Foo) {
match (*self, y) {
(Foo::Foo1(ref mut a), b) if (b == 5) => {
print!("is five");
*a = b + 42;

(b, self)
}

(Foo::Foo2(ref mut a), b) if (b == 5) => {
print!("is five");
*a = (b + 42) as i16;

(*a * b, self)
}

_ => {
print!("is not five!");
(y, self)
}
}
}
}



I feel like I would need a match arm such as the following, but it doesn't seem to be valid syntax:


(ref mut f @ Foo::Foo1, b) if (b == 5) => {
print!("is five");
f.0 = b + 42;
(b, f)
}


error[E0532]: expected unit struct/variant or constant, found tuple variant `Foo::Foo1`
--> src/main.rs:24:30
|
24 | (ref mut f @ Foo::Foo1, b) if (b == 5) => {
| ^^^^^^^^^ not a unit struct/variant or constant





"It seems perfectly safe", my C code seems perfectly safe too, but rust is not about "seems" but "is"
– Stargateur
41 secs ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

qZOtLv F ETorFd 6DCcnMSAZL
IsIr4FvK R4jrXW3kDfXVjK d d5KxoHlGWQkJh5,7BH7,pkvNatYG kfu AQ LGeiQ kBe7wHBAG5F2jAAWWYKf06T3dWvFxvyQz,VbvoX

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications