From the course: Rust Essential Training

Unlock the full course today

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

Multiple lifetime annotations

Multiple lifetime annotations - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Multiple lifetime annotations

- [Instructor] In the previous video, we saw that this program fails to compile because the lifetime of the return value from the best_fuel function on line one is annotated to be the most restrictive of the two input parameter lifetimes and the borrow checker detects that the propellant variable declared on line 13 doesn't live long enough to potentially be referenced as the result on line 16. Now, let's see what happens if we remove the possibility that our best_fuel function could ever return the second input parameter by changing line five from y to x. The function is still using the value referenced by y for the comparison on line two but then it just ignores the result of that comparison to always return x. Let's try running this program and it still fails to compile with the same error as before, because propellant2 goes out of scope before line 16 prints the result. But it's obvious here that propellant2…

Contents