From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Lifetime elision rules

Lifetime elision rules - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Lifetime elision rules

- [Tutor] You might be wondering why we've been able to write Rust functions throughout this course without needing to explicitly annotate lifetimes. Then when we reach this chapter suddenly the compiler starts requiring it. For example, the code shown here is from an earlier video about using slices as parameters. The get_first_word function on line seven has borrowed string slices as its input and output parameters. But this program will still compile and run just fine without lifetime annotations. Back in the early days of Rust before it even reached version 1.0, the compiler required the programmer to explicitly annotate the lifetime for every parameter that was a reference. So back then this get_first_word function would have had to define a lifetime, tick A and then associate that with the input parameter as well as the return parameter. Requiring those annotations made sense at the time, because it aligned…

Contents