Closed
Description
For example:
#[allow(dead_code)]
enum A {
V1(u8),
V2(u32),
}
fn main() {
let a = Some(Box::new(A::V1(1u8)));
if let Some(b) = a.as_ref() {
if let A::V1(v) = b {
println!("{:?}", v);
}
}
}
The rust 1.40 compiler will failed to compile this code and tell me:
error[E0308]: mismatched types
--> src/main.rs:11:16
|
11 | if let A::V1(v) = b {
| ^^^^^^^^ -
| | |
| | this match expression has type `std::boxed::Box<A>`
| | help: consider dereferencing the boxed value: `*b`
| expected struct `std::boxed::Box`, found enum `A`
|
= note: expected type `std::boxed::Box<A>`
found type `A`
But unfortunately it's wrong. If I replace b
by *b
, it will failed again and tell me:
error[E0308]: mismatched types
--> src/main.rs:11:16
|
11 | if let A::V1(v) = *b {
| ^^^^^^^^ --
| | |
| | this match expression has type `std::boxed::Box<A>`
| | help: consider dereferencing the boxed value: `**b`
| expected struct `std::boxed::Box`, found enum `A`
|
= note: expected type `std::boxed::Box<A>`
found type `A`
Although the second prompt is correct and it works. But I think using b.as_ref()
is the better idea.