learning-rust / learning-rust.github.io

Rust Programming Language Tutorials for Everyone!
https://learning-rust.github.io
MIT License
1.48k stars 165 forks source link

Question regarding a section in lifetimes #41

Closed BenMaruchu closed 4 years ago

BenMaruchu commented 4 years ago

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?

FedericoPonzi commented 4 years ago

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.

dumindu commented 4 years ago

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 :)

BenMaruchu commented 4 years ago

Thanks @dumindu , @FedericoPonzi for the answers.