Rust Lifetimes

At the time of writing this, this is the weirdest rust syntax I’ve found so far:

pub struct ExportDialogMessageData<'a> {
	pub portfolio: &'a PortfolioMessageHandler,
}

The 'a here. What the fuck is that?

It’s a generic representing which lifetime the struct should belong to. Lifetimes are a way of describing the scope for which a reference is valid. It’s an important part of Rust’s ownership model.

So in the above snippet, the ExportDialogMessageData contains a reference to a PortfolioMessageHandler, which is also bound to 'a. This guaranties that the referenced portfolio field must not outlive the lifetime 'a.

Currently, I have no idea how this is useful.

end of storey Last modified: