3 ms·
Here’s another example: fn f<T>() -> T { loop { println!(“Looping”); } } This won’t compile. How do you express to the compiler that this is
by jadenPete 20d ago
Here’s another example:
fn f<T>() -> T {
loop {
println!(“Looping”);
}
}
This won’t compile. How do you express to the compiler that this is correct (because the function never completes) without a never type?
Rust also doesn’t support generic function types, so a callback function returning `!` is possible, whereas one accepting a type argument and returning that type is not. You could do something clever with traits, but that requires extra work.
A lot of these things are achievable without `!`, but the compiler still needs that type internally and it pieces so nicely into the type system that I don’t see the point of bending over backwards to avoid it.