Skip to content

Commit a022210

Browse files
committed
lifetimes
1 parent b4ffaba commit a022210

6 files changed

Lines changed: 102 additions & 55 deletions

File tree

content/en/docs/c3.lifetimes.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,53 @@ title: Lifetimes
33
slug: lifetimes
44
---
55

6-
> [!caution]
7-
> This needs a refresh!
6+
> [!recap]
7+
> - Ownership: Each piece of data has a single owner, and data is only scoped to its owner (unless it is borrowed).
8+
> - In Rust, borrowing is the act of creating a reference to a value without copying the data and without taking ownership (move). While a reference exists, the original owner retains ownership, but its access to the data is restricted (or completely locked if `&mut`) until that reference’s last use.
9+
> - Liveness:
10+
> - Owned values (`T`) are dropped at the end of their scope (unless moved early).
11+
> - References (`&T`, `&mut T`) effectively end/ expire at their last use. This is known as Non-Lexical Lifetimes (NLL).
12+
> - Lifetimes are Rust's way of guaranteeing that a reference is valid within a discrete region of code at compile time.
813
9-
When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.
14+
## Lifetimes
1015

11-
Think,
16+
- A lifetime is a construct the Rust compiler's borrow checker uses to track how long references remain valid and every reference in Rust has a lifetime.
17+
- However, most local lifetimes are implicitly figured out by the compiler through a mechanism called lifetime elision.
18+
- The primary purpose of lifetimes is to prevent dangling references, ensuring data is never dropped while a pointer still looks at it.
1219

13-
- We have a variable binding, `a`.
14-
- We are referencing the value of `a`, from another variable binding `x`.
15-
16-
We have to make sure that `a` lives until we stop using `x`.
20+
Annotating a lifetime does not extend the lifespan of an actual value. It is simply a contract that tells the compiler, "The lifespan of this reference is guaranteed to be tied to the lifespan of this data."
1721

18-
> [!recap]
19-
> **Memory management** is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of
20-
> programming languages used Manual Memory Management which requires the programmer to give manual instructions to
21-
> identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented Garbage collection\(GC\),
22-
> a form of Automatic Memory Management\(AMM\). It determines what memory is no longer used and frees it automatically instead
23-
> of relying on the programmer. However, Objective-C and Swift provide similar functionality through Automatic Reference Counting\(ARC\).
22+
Lifetimes are declared with a single leading apostrophe `'`. By convention, lowercase letters are used starting from `'a`, moving alphabetically if multiple lifetimes are required.
2423

24+
```rust
25+
fn main() {
26+
let a = "A";
2527

26-
## What is Lifetime?
28+
take_str(a);
29+
take_str_elided(a);
30+
}
2731

28-
In Rust,
32+
fn take_str<'a>(x: &'a str) {
33+
println!("{x}");
34+
}
2935

30-
* A resource can only have **one owner** at a time. When it goes **out of the scope**, Rust removes it from the Memory.
31-
* When we want to reuse the same resource, we are **referencing** it/ **borrowing** its content.
32-
* When dealing with **references**, we have to specify **lifetime annotations** to provide instructions for the **compiler** to set **how long** those referenced resources **should be alive**.
33-
* ⭐ But because of lifetime annotations make the **code more verbose**, in order to make **common patterns** more ergonomic, Rust allows lifetimes to be **elided/omitted** in `fn` definitions. In this case, the compiler assigns lifetime annotations **implicitly**.
36+
fn take_str_elided(x: &str) {
37+
println!("{x}");
38+
}
39+
```
3440

35-
Lifetime annotations are **checked at compile-time**. The compiler checks when data is used for the first and the last times.
41+
`'static` is a special lifetime that indicating the reference can live for the entire duration of the program execution.
3642

37-
> * Unlike C and C++, **usually**, Rust doesn’t require explicitly dropping values at all.
38-
> * Unlike GC, Rust doesn’t place deallocation calls where the data is no longer referenced.
39-
> * Rust places deallocation calls where the data is about to go out of the scope and then enforces that no references to that resource exist after that point.
43+
```rust
44+
fn main() {
45+
let a = "A";
46+
take_str(a);
47+
}
4048

49+
fn take_str(x: &'static str) {
50+
println!("{x}");
51+
}
52+
```
4153

4254
## Usage
4355

docs/docs/index.xml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,30 +468,40 @@ __ &lt;a href="https://wiki.haskell.org/Combinator" target="_blank" &gt;wiki.has
468468
&lt;p&gt;This can be any form of &lt;code&gt;self&lt;/code&gt;, &lt;code&gt;&amp;amp;self&lt;/code&gt;, &lt;code&gt;&amp;amp;mut self&lt;/code&gt;, &lt;code&gt;self: Box&amp;lt;Self&amp;gt;&lt;/code&gt;, &lt;code&gt;self: Pin&amp;lt;&amp;amp;mut Self&amp;gt;&lt;/code&gt;, etc.&lt;/p&gt;</description></item><item><title>Installation</title><link>https://learning-rust.github.io/docs/installation/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/installation/</guid><description>&lt;h2 id="rustup"&gt;Rustup&lt;/h2&gt;
469469
&lt;p&gt;There are many ways to install Rust on your system. For the moment the official way to install Rust is using &lt;a href="https://rustup.rs/" target="_blank" &gt;Rustup&lt;/a&gt;.&lt;/p&gt;
470470
&lt;p&gt;&lt;a href="https://rust-lang.github.io/rustup/index.html" target="_blank" &gt;📖&lt;/a&gt; Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between &lt;strong&gt;stable, beta, and nightly&lt;/strong&gt; compilers and keep them updated. It also makes cross-compiling simpler with binary builds of the standard library for common platforms.&lt;/p&gt;
471-
&lt;p&gt;&lt;a href="https://rust-lang.github.io/rustup/installation/index.html" target="_blank" &gt;📖&lt;/a&gt; Rustup installs &lt;code&gt;rustc&lt;/code&gt;, &lt;code&gt;cargo&lt;/code&gt;, &lt;code&gt;rustup&lt;/code&gt; and other standard tools to &lt;strong&gt;Cargo&amp;rsquo;s &lt;code&gt;bin&lt;/code&gt; directory&lt;/strong&gt;. On Unix it is located at &lt;code&gt;$HOME/.cargo/bin&lt;/code&gt; and on Windows at &lt;code&gt;%USERPROFILE%\.cargo\bin&lt;/code&gt;. This is the same directory that &lt;code&gt;cargo install&lt;/code&gt; will install Rust programs and Cargo plugins.&lt;/p&gt;</description></item><item><title>Lifetimes</title><link>https://learning-rust.github.io/docs/lifetimes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/lifetimes/</guid><description>&lt;div class="alert alert-caution"&gt;
471+
&lt;p&gt;&lt;a href="https://rust-lang.github.io/rustup/installation/index.html" target="_blank" &gt;📖&lt;/a&gt; Rustup installs &lt;code&gt;rustc&lt;/code&gt;, &lt;code&gt;cargo&lt;/code&gt;, &lt;code&gt;rustup&lt;/code&gt; and other standard tools to &lt;strong&gt;Cargo&amp;rsquo;s &lt;code&gt;bin&lt;/code&gt; directory&lt;/strong&gt;. On Unix it is located at &lt;code&gt;$HOME/.cargo/bin&lt;/code&gt; and on Windows at &lt;code&gt;%USERPROFILE%\.cargo\bin&lt;/code&gt;. This is the same directory that &lt;code&gt;cargo install&lt;/code&gt; will install Rust programs and Cargo plugins.&lt;/p&gt;</description></item><item><title>Lifetimes</title><link>https://learning-rust.github.io/docs/lifetimes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/lifetimes/</guid><description>&lt;div class="alert alert-recap"&gt;
472472
&lt;div class="alert-header"&gt;
473473

474474

475475

476-
&lt;svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px"&gt;&lt;path d="M480-300q15 0 25.5-10.5T516-336q0-15-10.5-25.5T480-372q-15 0-25.5 10.5T444-336q0 15 10.5 25.5T480-300Zm25.5-142.5Q516-453 516-468v-168q0-15-10.5-25.5T480-672q-15 0-25.5 10.5T444-636v168q0 15 10.5 25.5T480-432q15 0 25.5-10.5ZM371-144q-14 0-27-5t-24-16L165-321q-10-10-15.5-23.5T144-372v-217q0-14 5-27t16-24l155-155q11-11 24-16t27-5h218q14 0 27 5t24 16l155 155q11 11 16 24t5 27v218q0 14-5 27t-16 24L639-165q-10 10-23.5 15.5T588-144H371Zm0-72h218l155-155v-218L588-744H371L216-589v218l155 155Zm109-264Z"/&gt;&lt;/svg&gt;
476+
&lt;svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px"&gt;&lt;path d="m687-273-78-160 95-95-79.77-79.77v-112.46H511.77L432-800l-79.77 79.77H239.77v112.46L160-528l79.77 79.77v112.46h112.46L432-256l95-95 160 78Zm104 104q-8 8-19 10.5t-22-3.5L541-264l-84 84q-10.64 11-24.82 11T407-180l-84.26-84H204q-15.3 0-25.65-10.35Q168-284.7 168-300v-118.74L84-503q-11-10.64-11-24.82T84-553l84-84.26V-756q0-15.3 10.35-25.65Q188.7-792 204-792h118.74L407-876q10.64-11 24.82-11T457-876l84.26 84H660q15.3 0 25.65 10.35Q696-771.3 696-756v118.74L780-553q11 10.64 11 24.82T780-503l-84 84 102 209q6 11 3.5 22T791-169ZM432-528Z"/&gt;&lt;/svg&gt;
477477

478478

479479
&lt;span&gt;
480480

481-
Caution
481+
Recap
482482

483483
&lt;/span&gt;
484484
&lt;/div&gt;
485485
&lt;div class="alert-body"&gt;
486-
&lt;p&gt;This needs a refresh!&lt;/p&gt;
486+
&lt;ul&gt;
487+
&lt;li&gt;Ownership: Each piece of data has a single owner, and data is only scoped to its owner (unless it is borrowed).&lt;/li&gt;
488+
&lt;li&gt;In Rust, borrowing is the act of creating a reference to a value without copying the data and without taking ownership (move). While a reference exists, the original owner retains ownership, but its access to the data is restricted (or completely locked if &lt;code&gt;&amp;amp;mut&lt;/code&gt;) until that reference’s last use.&lt;/li&gt;
489+
&lt;li&gt;Liveness:
490+
&lt;ul&gt;
491+
&lt;li&gt;Owned values (&lt;code&gt;T&lt;/code&gt;) are dropped at the end of their scope (unless moved early).&lt;/li&gt;
492+
&lt;li&gt;References (&lt;code&gt;&amp;amp;T&lt;/code&gt;, &lt;code&gt;&amp;amp;mut T&lt;/code&gt;) effectively end/ expire at their last use. This is known as Non-Lexical Lifetimes (NLL).&lt;/li&gt;
493+
&lt;/ul&gt;
494+
&lt;/li&gt;
495+
&lt;li&gt;Lifetimes are Rust&amp;rsquo;s way of guaranteeing that a reference is valid within a discrete region of code at compile time.&lt;/li&gt;
496+
&lt;/ul&gt;
487497
&lt;/div&gt;
488-
&lt;/div&gt;&lt;p&gt;When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.&lt;/p&gt;
489-
&lt;p&gt;Think,&lt;/p&gt;
498+
&lt;/div&gt;&lt;h2 id="lifetimes"&gt;Lifetimes&lt;/h2&gt;
490499
&lt;ul&gt;
491-
&lt;li&gt;We have a variable binding, &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
492-
&lt;li&gt;We are referencing the value of &lt;code&gt;a&lt;/code&gt;, from another variable binding &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
500+
&lt;li&gt;A lifetime is a construct the Rust compiler&amp;rsquo;s borrow checker uses to track how long references remain valid and every reference in Rust has a lifetime.&lt;/li&gt;
501+
&lt;li&gt;However, most local lifetimes are implicitly figured out by the compiler through a mechanism called lifetime elision.&lt;/li&gt;
502+
&lt;li&gt;The primary purpose of lifetimes is to prevent dangling references, ensuring data is never dropped while a pointer still looks at it.&lt;/li&gt;
493503
&lt;/ul&gt;
494-
&lt;p&gt;We have to make sure that &lt;code&gt;a&lt;/code&gt; lives until we stop using &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Modules</title><link>https://learning-rust.github.io/docs/modules/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/modules/</guid><description>&lt;div class="alert alert-caution"&gt;
504+
&lt;p&gt;Annotating a lifetime does not extend the lifespan of an actual value. It is simply a contract that tells the compiler, &amp;ldquo;The lifespan of this reference is guaranteed to be tied to the lifespan of this data.&amp;rdquo;&lt;/p&gt;</description></item><item><title>Modules</title><link>https://learning-rust.github.io/docs/modules/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/modules/</guid><description>&lt;div class="alert alert-caution"&gt;
495505
&lt;div class="alert-header"&gt;
496506

497507

0 commit comments

Comments
 (0)