O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Lifetimes

The third piece in the Rust memory safety puzzle is lifetimes. If you have ever programmed in C, you should be acutely aware of the lifetime issue: every time you allocate some variable with malloc, it should have an explicit owner and that owner should reliably decide when that variable's life ends. It's not codified anywhere; rather it's the programmer's responsibility.

In Rust every reference has a lifetime attached to it. A lifetime defines how long the reference lives in relation to other references. Whenever it's able to, the Rust compiler juggles with them without the programmer's help via a mechanism called lifetime elision. Sometimes it's not able to, however, and then it needs our help.

Here's a list of all the places ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required