implement Deref
for Bar
This commit is contained in:
parent
5924ef874e
commit
efe438b474
2 changed files with 30 additions and 2 deletions
|
@ -1,7 +1,21 @@
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
v: Vec<u32>,
|
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) {
|
fn f(foo: &Foo) {
|
||||||
match foo {
|
match foo {
|
||||||
Foo { v: [1, 2] } => {}
|
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() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
error[E0529]: expected an array or slice, found `Vec<u32>`
|
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] } => {}
|
LL | Foo { v: [1, 2] } => {}
|
||||||
| ^^^^^^ pattern cannot match with input type `Vec<u32>`
|
| ^^^^^^ 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`.
|
For more information about this error, try `rustc --explain E0529`.
|
||||||
|
|
Loading…
Add table
Reference in a new issue