January 2019
Beginner to intermediate
554 pages
13h 31m
English
We can specify relation between lifetimes that specifies whether two references can be used in the same place. Continuing with our Decoder struct example, we can specify the lifetimes' relations with each other in the impl block, like so:
// lifetime_subtyping.rsstruct Decoder<'a, 'b, S, R> { schema: &'a S, reader: &'b R}impl<'a, 'b, S, R> Decoder<'a, 'b, S, R>where 'a: 'b { }fn main() { let a: Vec<u8> = vec![]; let b: Vec<u8> = vec![]; let decoder = Decoder {schema: &a, reader: &b};}
We specified the relation in the impl block using the where clause as: 'a: 'b . This is read as the lifetime 'a outlives 'b or in other words 'b should never live longer than 'a.
Read now
Unlock full access