learning-rust/learning-rust.github.io

Question regarding a section in lifetimes

Closed this issue · 3 comments

Hello,

Thanks everyone for this amazing resource for learning rust. I have a question regarding a section in lifetime section.

Lifetime annotations are checked at compile-time. Compiler checks when a data is used for the first and the last times. According to that, Rust manages memory in run time. This is the major reason for slower compilation times in Rust.

was this suppose to say Rust manages memory at compile time ... and not ...in run time... based on the context of the statement?

Actually the statement is correct. At compile time rustc checks the lifetime annotations. Lifetimes allows for safe memory management at runtime. Basically at compile time it checks if a variable will be used afterwards or can be fred after for instance exiting a function. According to that "plan", at runtime the variable will be fred.

Sorry for the late reply.

In Rust, lifetime annotations are checked at compile-time, not in run-time. But according to the annotation checks, it tweaks things while compiling for run-time. So, it can guarantee the memory safety at run-time.

Hope these answers were helped to remove your confusions. However, use Rust's sub Reddit or official communication channels for a quick response next time :)

Thanks @dumindu , @FedericoPonzi for the answers.