I'm a newbie in Rust and I'm still struggling with lifetimes in Rust. Below is an example from a book I'm reading. Could anyone help explain why the author can get this information just by looking at the function signature? I already have basic understanding of borrowing, references etc. but still can't understand it.
For example, suppose we have a parsing function that takes a slice of bytes, and returns a structure holding the results of the parse:
fn parse_record<'i>(input: &'i [u8]) -> Record<'i> { ... }
Without looking into the definition of the Record type at all, we can tell that, if we receive a Record from parse_record, whatever references it contains must point into the input buffer we passed in, and nowhere else (except perhaps at 'static values).
'i. Since Rust disallows returning reference to a local variable ANDRecordhas the same lifetime asinputit must mean that whatever is in theRecordmust come frominput. - GrayCatRecordshould live as long as the value in a scope we name'i, we getRecord<'i>. - Optimistic Peach