To give some background why something like None
is actually typed, consider the following code
fn unwrap_or_default<T: Default>(v: Option<T>) -> T {
match v {
Some(v) => v,
None => T::default(),
}
}
let x: Option<u32> = None;
let y: Option<String> = None;
println!("{}", unwrap_or_default(x)); // Calls into u32's Default impl
println!("{}", unwrap_or_default(y)); // Calls into String's Default impl
println!("{}", unwrap_or_default(None)); // Impossible to know which Default impl should be used here
For structs/etc it’s possible to give default generic types but for functions this is unfortunately not possible yet/anymore. Tracking issue for `invalid_type_param_default` compatibility lint · Issue #36887 · rust-lang/rust · GitHub has some background about that.