Hacker News new | ask | show | jobs
by kibwen 1327 days ago
It's also possible to have `&str`s with smaller lifetimes than `'static`, in which case you will have a different lifetime specifier there.
1 comments

Ah, so this probably tells the compiler where to insert the free? If I gave it a local lifetime it would free the memory on function return for example, even if I tried to return a reference to it? Basically Rust just forces you to code in the memory freeing at the definition state of variable creation?
Not quite. Lifetime annotations prevent you from accidentally using references after the value they refer to has been freed. They track how long things will live, instead of defining how long they will live.

Basically they turn use-after-free errors into compile errors.

(I'm using 'free' here to mean cleaned up in general. Lifetimes can track stack values.)