How it works...
In this recipe, we created a simple statistics toolbox that allows normal distribution samples to be analyzed quickly and accurately. However, this example was only chosen to illustrate the ways of how lifetimes are useful and comparatively simple. In Step 2, we are creating a function to calculate the mean of a given collection. Since lifetimes can be inferred from using the functions/variables, explicit specification of the lifetime is optional. Nevertheless, the function explicitly ties the input parameter's lifetime to the function's lifetime, requiring any reference that is passed in to outlive mean().
Steps 3 and 4 show how to deal with lifetimes in structs and their implementations. Since type instances can easily outlive ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access