I'm new to Rust. Below is my testing.
#[derive(Debug)]
enum Food {
Cake,
Pizza,
Salad,
}
#[derive(Debug)]
struct Bag {
food: Food
}
fn main() {
let bag = Bag { food: Food::Cake };
match bag.food {
Food::Cake => println!("I got cake"),
x => println!("I got {:?}", x)
}
println!("{:?}", bag);
}
When I run it, I got an error.
error[E0382]: borrow of moved value: `bag`
--> src\main.rs:20:22
|
17 | x => println!("I got {:?}", x)
| - value moved here
...
20 | println!("{:?}", bag);
| ^^^ value borrowed here after partial move
|
= note: move occurs because `bag.food` has type `Food`, which does not implement the `Copy` trait
It's clear that bag.food
will not match x
arm in the code. Why a move happens there?