I can change the last element of a vec:
#[derive(Debug)]
enum Type {
A,
B,
C,
}
fn main() {
let mut v = vec![Type::A, Type::B, Type::B];
match v.last_mut(){
None => v.push(Type::A),
Some(last) => *last = Type::C,
}
println!("{:?}", v)
}
==> prints [A, B, C]
.
But if my enum constants have data, I can't seem to capture them anymore... For example this compiles:
#[derive(Debug)]
enum Type {
A(i32),
B(i32),
C(i32),
}
fn main() {
let mut v = vec![Type::A(0), Type::B(1), Type::B(2)];
match v.last_mut(){
None => v.push(Type::A(0)),
Some(last @ Type::B(_)) => *last = Type::C(42),
Some(Type::A(_)) | Some(Type::C(_)) => {}
}
println!("{:?}", v);
}
==> prints [A(0), B(1), C(42)]
This worked just because I put _
inside Type::B(_)
pattern.
But if I try to capture it to use in Type::C()
with this:
Some(last @ Type::B(p)) => *last = Type::C(*p),
I get these three errors:
error: borrow of moved value
--> src/main.rs:13:14
|
13 | Some(last @ Type::B(p)) => *last = Type::C(*p),
| ----^^^^^^^^^^^-^
| | |
| | value borrowed here after move
| value moved into `last` here
| move occurs because `last` has type `&mut Type` which does not implement the `Copy` trait
error[E0658]: pattern bindings after an `@` are unstable
--> src/main.rs:13:29
|
13 | Some(last @ Type::B(p)) => *last = Type::C(*p),
| ^
|
= note: see issue #65490 <https://github.com/rust-lang/rust/issues/65490> for more information
error[E0382]: borrow of moved value
--> src/main.rs:13:29
|
13 | Some(last @ Type::B(p)) => *last = Type::C(*p),
| ---------------^-
| | |
| | value borrowed here after move
| value moved here
|
= note: move occurs because value has type `&mut Type`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving the value
|
13 | Some(ref last @ Type::B(p)) => *last = Type::C(*p),
| ^^^
error: aborting due to 3 previous errors
How could I make this work, i.e. capture the current value of B (an &mut i32
), and pass it to C?
Capturing last
mutably and the value inside it immutably would mean that you hold both a mut
reference and a shared reference to the same data at the same time. Rust's ownership model prohibits such aliasing, so the borrow checker rejects it.
How could I make this work, i.e. capture the current value of B (a
&mut i32
), and pass it to C?
You can capture the last value and examine it in a separate inner match. This avoids the initial request for aliased references which is a problem for Rust. Instead, it creates the inner p
reference from the outer last
reference, which is allowed as long as the outer reference is not used during the lifetime of the inner reference. In other words, this compiles:
match v.last_mut() {
None => v.push(Type::A(0)),
Some(last) => {
match last {
Type::B(p) => *last = Type::C(*p),
Type::A(_) | Type::C(_) => (),
}
}
}