As the title says, my question is why doesn't the compiler complain about a possible "None" variant of the Option type when it is passed as a value to the enum field that expects an i32 value?
enum AmazingEnum {
V1,
V2 { something: i32 },
}
impl AmazingEnum {
fn do_something(some_arg: &AmazingEnum) {
match some_arg {
AmazingEnum::V2 { something, .. } => {
println!("Matched V2, this is a \"something\" field: {something}")
}
AmazingEnum::V1 => (),
}
}
}
fn main() {
// deliberately specifying as "None" for the sake of example:
let another_value: Option<i32> = None;
// this compiles just fine
let enum_instance = AmazingEnum::V2 {
something: another_value.unwrap(), // compiler doesn't complain about the value possibly being "None" while the enum expects the value to be of "i32" type
};
// but since the above compiles just fine, the following code panics during runtime:
AmazingEnum::do_something(&enum_instance);
}
Obviously, we have "unwrap_or", "unwrap_or_default", etc.., to ensure that a value is not "None" when it needs to be "Some"thing, but since, for example, a match clause does not compile unless all possible variants of the enum are accounted for, wouldn't it also be suitable for a compiler to check possible erroneous variants of an Option enum passed to a field the type of which is concrete and known at compile time?