From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Lifetime annotation syntax - Rust Tutorial
From the course: Rust Essential Training
Lifetime annotation syntax
- [Instructor] There are certain situations where the Rust compiler will require you to explicitly annotate the lifetime of variables. Consider the best fuel function on line one which when given two options determines which one is the best type of rocket fuel. It accepts two string references as input representing the fuels to compare, and then an if else expression compares the length of the two strings slices and returns whichever one is longer. I'll admit it's not a scientific way to evaluate the quality of rocket fuel, but it works for a simple programming example. Down in the main function, we initialize strings for the two types of propellant past references to those strings to the best fuel function, and then print the result. There's nothing tricky going on here, and both propellant strings will still be alive and in scope when they're borrowed for the best fuel function and when the result is printed. So it…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.