|
|
@ -25,21 +25,23 @@ mod a {
|
|
|
|
pub struct S {
|
|
|
|
pub struct S {
|
|
|
|
pub foo: i32,
|
|
|
|
pub foo: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[non_exhaustive]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum AdmitMoreVariants {
|
|
|
|
pub enum AdmitMoreVariants {
|
|
|
|
VariantA,
|
|
|
|
VariantA,
|
|
|
|
VariantB,
|
|
|
|
VariantB,
|
|
|
|
#[non_exhaustive]
|
|
|
|
#[non_exhaustive]
|
|
|
|
VariantC { a: String }
|
|
|
|
VariantC {
|
|
|
|
|
|
|
|
a: String,
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn print_matched_variants(s: a::S) {
|
|
|
|
fn print_matched_variants(s: a::S) {
|
|
|
|
// Because S is `#[non_exhaustive]`, it cannot be named here and
|
|
|
|
// Because S is `#[non_exhaustive]`, it cannot be named here and
|
|
|
|
// we must use `..` in the pattern.
|
|
|
|
// we must use `..` in the pattern.
|
|
|
|
let a::S { foo: _, ..} = s;
|
|
|
|
let a::S { foo: _, .. } = s;
|
|
|
|
|
|
|
|
|
|
|
|
let some_enum = a::AdmitMoreVariants::VariantA;
|
|
|
|
let some_enum = a::AdmitMoreVariants::VariantA;
|
|
|
|
match some_enum {
|
|
|
|
match some_enum {
|
|
|
|
a::AdmitMoreVariants::VariantA => println!("it's an A"),
|
|
|
|
a::AdmitMoreVariants::VariantA => println!("it's an A"),
|
|
|
@ -50,7 +52,7 @@ fn print_matched_variants(s: a::S) {
|
|
|
|
|
|
|
|
|
|
|
|
// The wildcard match is required because more variants may be
|
|
|
|
// The wildcard match is required because more variants may be
|
|
|
|
// added in the future
|
|
|
|
// added in the future
|
|
|
|
_ => println!("it's a new variant")
|
|
|
|
_ => println!("it's a new variant"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -78,7 +80,7 @@ pub struct S {
|
|
|
|
pub a: i32,
|
|
|
|
pub a: i32,
|
|
|
|
// Because `b` is private, you cannot match on `S` without using `..` and `S`
|
|
|
|
// Because `b` is private, you cannot match on `S` without using `..` and `S`
|
|
|
|
// cannot be directly instantiated or matched against
|
|
|
|
// cannot be directly instantiated or matched against
|
|
|
|
_b: ()
|
|
|
|
_b: (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|