Well, not really. But still
2024-11-03
Apparently there is a type in Rust we don’t really talk about. Or think about.
In Rust, an expression has to return the same type in all paths. This won’t work:
fn mismatched(i: i32) -> String {
if i == 0 {
return "i is zero".to_string();
}
return 1;
} // error[E0308]: mismatched types
let my_cond = if true { 2 } else { "whatever".to_string() }; // error[E0308]: `if` and `else` have incompatible types
Then why does this work?
let some_b = false;
match some_b {
true => true,
false => panic!("It was not true");
}
Well, apparently it’s because of the never type
(aka !
).
These paths never resolve to any value.
Similarly:
let some_b = false;
match some_b {
true => true,
false => std::process::exit(1),
};
let mut i = 0;
while i < 100 {
i += 1;
let _ = match i {
1..=99 => {
println!("Num is: {}", i);
i
}
_ => break,
};
}
So, whenever there are code paths that don’t return the same type, that’s probably because they’re returning a never
.