Rust contravariance, Regarding the contravariance of `fn(&'a i32) -> ()`

We can't. If we use fn(&'a i32), it means that we can handle this short-lived lifetime - e.g. we won't shove it into thread-local to live forever (since it would dangle). If we have fn(&'static i32) though, it can treat this reference as "living forever", and therefore we can't use it in place where &'a i32 is passed.

On the other hand, if we have &'static i32, we can pass it to fn(&'a i32) - the function would simply not use the extra capabilities. Therefore, fn(&'a i32) can be used where fn(&'static i32) is expected.

1 Like