January 2019
Intermediate to advanced
316 pages
8h 8m
English
Some lifetimes are different and Rust denominates them with a '. While this could be the predefined 'static, it's equally possible to create your own, something that is often required when working with structures.
This makes sense when thinking about the underlying memory structure: if an input parameter is passed into the function and returned at the end, its lifetime surpasses the function's. While the function owns this part of the memory during its lifetime, it cannot borrow a variable for longer than it actually exists. So, this snippet cannot work:
fn another_function(mut passing_through: MyStruct) -> MyStruct { let x = vec![1, 2, 3]; // passing_through cannot hold a reference // to a shorter lived x! // the ...