implement Deref for Bar

This commit is contained in:
Takayuki Maeda 2022-04-18 13:08:23 +09:00
parent 5924ef874e
commit efe438b474
2 changed files with 30 additions and 2 deletions

View file

@ -1,7 +1,21 @@
use std::ops::Deref;
struct Foo {
v: Vec<u32>,
}
struct Bar {
v: Vec<u32>,
}
impl Deref for Bar {
type Target = Vec<u32>;
fn deref(&self) -> &Self::Target {
&self.v
}
}
fn f(foo: &Foo) {
match foo {
Foo { v: [1, 2] } => {}
@ -10,4 +24,12 @@ fn f(foo: &Foo) {
}
}
fn bar(bar: &Bar) {
match bar {
Bar { v: [1, 2] } => {}
//~^ ERROR expected an array or slice, found `Vec<u32>
_ => {}
}
}
fn main() {}

View file

@ -1,9 +1,15 @@
error[E0529]: expected an array or slice, found `Vec<u32>`
--> $DIR/pattern-struct-with-slice-vec-field.rs:7:18
--> $DIR/pattern-struct-with-slice-vec-field.rs:21:18
|
LL | Foo { v: [1, 2] } => {}
| ^^^^^^ pattern cannot match with input type `Vec<u32>`
error: aborting due to previous error
error[E0529]: expected an array or slice, found `Vec<u32>`
--> $DIR/pattern-struct-with-slice-vec-field.rs:29:18
|
LL | Bar { v: [1, 2] } => {}
| ^^^^^^ pattern cannot match with input type `Vec<u32>`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0529`.