The following code (playground)
enum Enum{
Variant(i32),
}
fn stuff(x: Enum) -> i32{
if let Enum::Variant(value) = x {
value
}
}
gives the following error:
Compiling playground v0.0.1 (/playground)
error[E0317]: if may be missing an else clause
--> src/lib.rs:6:5
|
6 | / if let Enum::Variant(value) = x {
7 | | value
8 | | }
| |_____^ expected (), found i32
|
= note: expected type `()`
found type `i32`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0317`.
error: Could not compile `playground`.
which is weird, since the else clause would never be visited even if it was present.
The equivalent one-arm match
enum Enum{
Variant(i32),
}
fn stuff(x: Enum) -> i32{
match x {
Enum::Variant(value) => value,
}
}
compiles just fine (expectedly).
Adding an else { unreachable!() } branch to the if let is, of course, possible, but unelegant, and really shouldn't be needed, as it can be inferred at compile time.
The following code (playground)
gives the following error:
which is weird, since the
elseclause would never be visited even if it was present.The equivalent one-arm match
compiles just fine (expectedly).
Adding an
else { unreachable!() }branch to theif letis, of course, possible, but unelegant, and really shouldn't be needed, as it can be inferred at compile time.