Sub topic 14
Higher-ranked types and subtyping
This chapter assumes you can read for<'a> bounds and know that fn(T) is contravariant in T. It adds one more subtyping rule: a higher-ranked type is a subtype of each of its less general forms.
Types can quantify over lifetimes
A function item such as fn len_of(s: &str) -> usize has no single fixed lifetime for s. Elision turns the signature into this:
fn len_of<'a>(s: &'a str) -> usize {
s.len()
}When you use len_of as a function pointer, its type is for<'a> fn(&'a str) -> usize. That reads as "for every lifetime 'a, a function from &'a str to usize". The type is higher-ranked because the quantifier sits inside the type, not on the surrounding function.
The subtyping rule
A higher-ranked type can be used where a less general type is expected. The compiler picks one lifetime for 'a, which is called instantiating the quantifier. The result is a subtype of the original.
fn takes_static(f: fn(&'static str) -> usize) -> usize {
f("static")
}
fn len_of(s: &str) -> usize {
s.len()
}
fn main() {
println!("{}", takes_static(len_of));
}takes_static only ever calls f with a &'static str. A function that accepts every lifetime also accepts 'static, so it is safe to pass. The program prints 6.
The reverse does not hold
Going the other way would let a function that only handles 'static strings receive a short-lived one. That would produce a dangling reference, so the compiler rejects it:
fn takes_any(f: for<'a> fn(&'a str) -> usize) -> usize {
let owned = String::from("temporary");
f(&owned)
}
fn only_static(s: &'static str) -> usize {
s.len()
}
fn main() {
// Does not compile: `only_static` has type fn(&'static str) -> usize,
// which is not a subtype of for<'a> fn(&'a str) -> usize.
// println!("{}", takes_any(only_static));
println!("{}", takes_any(|s| s.len()));
}The commented-out call fails with a "mismatched types" error. The non-capturing closure compiles because its parameter type is inferred from the for<'a> bound.
How this relates to contravariance
The two rules are separate, and they work together. Variance relates two types that differ in a lifetime. Higher-ranked subtyping removes a quantifier by choosing a lifetime.
The argument position of fn(&'static str) is contravariant, which explains one direction. Given 'long: 'short:
fn(&'short str)is a subtype offn(&'long str).- A function that accepts shorter borrows can stand in for one that only needs to accept longer ones.
Instantiating for<'a> at 'static is a special case of this idea. The higher-ranked type behaves as if it were the "most accepting" function, so it sits below every specific version.
Practical consequences
- Passing a named function or non-capturing closure where
F: Fn(&'static str)is expected works, because the general type coerces to the specific one. - Passing a function that fixes a lifetime where
for<'a> Fn(&'a T)is required fails. The error usually reads "implementation ofFnis not general enough". - Subtyping happens automatically at coercion sites. You never write the instantiation yourself.
Exercise
Is for<'a> fn(&'a str) -> usize a subtype of fn(&'static str) -> usize, the reverse, or both? Why?
Show answer
It is a subtype of fn(&'static str) -> usize only. Instantiating 'a = 'static gives the specific type. The reverse would let a function that handles only 'static strings receive a shorter-lived borrow, which would be unsound.