The following code won't compile:
#[derive(Debug)]
struct Foo {
x: i32,
}
pub fn main() {
let a = vec![Foo { x: 1 }, Foo { x: 2 }];
match a.get(0) {
Some(&x) => println!("ref {:?}", x),
None => {}
}
}
Throws this error:
error[E0507]: cannot move out of a shared reference
--> src/main.rs:8:11
|
8 | match a.get(0) {
| ^^^^^^^^
9 | Some(&x) => println!("ref {:?}", x),
| -
| |
| data moved here
| move occurs because `x` has type `Foo`, which does not implement the `Copy` trait
I thought std::Vec<T>::get
returns Some(&T)
on success. Doesn't that mean an ampersand should be used in pattern matching?