1 % Higher-Rank Trait Bounds (HRTBs)
3 Rust's `Fn` traits are a little bit magic. For instance, we can write the
13 where F: Fn(&(u8, u16)) -> &u8,
15 fn call(&self) -> &u8 {
16 (self.func)(&self.data)
20 fn do_it(data: &(u8, u16)) -> &u8 { &data.0 }
23 let clo = Closure { data: (0, 1), func: do_it };
24 println!("{}", clo.call());
28 If we try to naively desugar this code in the same way that we did in the
29 lifetimes section, we run into some trouble:
38 // where F: Fn(&'??? (u8, u16)) -> &'??? u8,
40 fn call<'a>(&'a self) -> &'a u8 {
41 (self.func)(&self.data)
45 fn do_it<'b>(data: &'b (u8, u16)) -> &'b u8 { &'b data.0 }
49 let clo = Closure { data: (0, 1), func: do_it };
50 println!("{}", clo.call());
55 How on earth are we supposed to express the lifetimes on `F`'s trait bound? We
56 need to provide some lifetime there, but the lifetime we care about can't be
57 named until we enter the body of `call`! Also, that isn't some fixed lifetime;
58 `call` works with *any* lifetime `&self` happens to have at that point.
60 This job requires The Magic of Higher-Rank Trait Bounds (HRTBs). The way we
61 desugar this is as follows:
64 where for<'a> F: Fn(&'a (u8, u16)) -> &'a u8,
67 (Where `Fn(a, b, c) -> d` is itself just sugar for the unstable *real* `Fn`
70 `for<'a>` can be read as "for all choices of `'a`", and basically produces an
71 *infinite list* of trait bounds that F must satisfy. Intense. There aren't many
72 places outside of the `Fn` traits where we encounter HRTBs, and even for
73 those we have a nice magic sugar for the common cases.