Skip to content

Commit 7930e18

Browse files
committed
Update some text about borrow checking for accuracy
1 parent 21ce5ab commit 7930e18

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

docs/docs/lifetimes/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html><html lang=en-US><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="Rust Programming Language Tutorials for Everyone!"><meta name=author content="Dumindu Madunuwan"><meta name=theme-color content="#ffffff" media="(prefers-color-scheme: light)"><meta name=theme-color content="#101010" media="(prefers-color-scheme: dark)"><title>Lifetimes · Learning Rust</title>
22
<link rel=canonical href=https://learning-rust.github.io/docs/lifetimes/><link rel=stylesheet href=/assets/css/docs.min.8e9408609771a441499aa5571a4585b0ca95783b842d5c758af5eef1457b0fe0.css integrity><link rel=manifest href=/manifest.json><link rel=icon href=/favicon/favicon.ico><link rel=icon href=/favicon/favicon-16x16.png sizes=16x16 type=image/png><link rel=icon href=/favicon/favicon-32x32.png sizes=32x32 type=image/png><link rel=apple-touch-icon href=/favicon/apple-touch-icon.png sizes=180x180><script async src="https://www.googletagmanager.com/gtag/js?id=G-FZHQCXSZ89"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-FZHQCXSZ89")</script></head><body><div id=outer-wrapper><div id=aside-wrapper><aside><div><button class=btn><i></i>Close</button></div><a href=https://learning-rust.github.io/ class=site-logo>Learning Rust</a><nav role=navigation><details open><summary>Documentation</summary><ul><li><a href=https://learning-rust.github.io/docs/overview/>Overview</a></li></ul></details><details open><summary>Basics</summary><ul><li><a href=https://learning-rust.github.io/docs/why-rust/>Why Rust?</a></li><li><a href=https://learning-rust.github.io/docs/installation/>Installation</a></li><li><a href=https://learning-rust.github.io/docs/hello-world/>Hello World</a></li><li><a href=https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/>Cargo, Crates and Basic Project Structure</a></li><li><a href=https://learning-rust.github.io/docs/comments-and-documenting-the-code/>Comments and Documenting the code</a></li><li><a href=https://learning-rust.github.io/docs/variable-bindings-constants-and-statics/>Variable bindings, Constants and Statics</a></li><li><a href=https://learning-rust.github.io/docs/functions/>Functions</a></li><li><a href=https://learning-rust.github.io/docs/primitive-data-types/>Primitive Data Types</a></li><li><a href=https://learning-rust.github.io/docs/operators/>Operators</a></li><li><a href=https://learning-rust.github.io/docs/control-flows/>Control Flows</a></li></ul></details><details open><summary>Beyond The Basics</summary><ul><li><a href=https://learning-rust.github.io/docs/vectors/>Vectors</a></li><li><a href=https://learning-rust.github.io/docs/structs/>Structs</a></li><li><a href=https://learning-rust.github.io/docs/enums/>Enums</a></li><li><a href=https://learning-rust.github.io/docs/generics/>Generics</a></li><li><a href=https://learning-rust.github.io/docs/impls-and-traits/>Impls and Traits</a></li></ul></details><details open><summary>The Tough Part</summary><ul><li><a href=https://learning-rust.github.io/docs/ownership/>Ownership</a></li><li><a href=https://learning-rust.github.io/docs/borrowing/>Borrowing</a></li><li><a class=active href=https://learning-rust.github.io/docs/lifetimes/>Lifetimes</a></li></ul></details><details open><summary>Let's Get It Started</summary><ul><li><a href=https://learning-rust.github.io/docs/code-organization/>Code Organization</a></li><li><a href=https://learning-rust.github.io/docs/functions-02/>Functions (02)</a></li><li><a href=https://learning-rust.github.io/docs/modules/>Modules</a></li><li><a href=https://learning-rust.github.io/docs/crates/>Crates</a></li><li><a href=https://learning-rust.github.io/docs/workspaces/>Workspaces</a></li><li><a href=https://learning-rust.github.io/docs/use/>Use</a></li><li><a href=https://learning-rust.github.io/docs/std-primitives-and-preludes/>STD, Primitives and Preludes</a></li></ul></details><details open><summary>Error Handling</summary><ul><li><a href=https://learning-rust.github.io/docs/smart-compiler/>Smart Compiler</a></li><li><a href=https://learning-rust.github.io/docs/panicking/>Panicking</a></li><li><a href=https://learning-rust.github.io/docs/option-and-result/>Option and Result</a></li><li><a href=https://learning-rust.github.io/docs/unwrap-and-expect/>Unwrap and Expect</a></li><li><a href=https://learning-rust.github.io/docs/error-and-none-propagation/>Error and None Propagation</a></li><li><a href=https://learning-rust.github.io/docs/combinators/>Combinators</a></li><li><a href=https://learning-rust.github.io/docs/custom-error-types/>Custom Error Types</a></li></ul></details></nav></aside></div><div id=content-wrapper><header><a href=https://learning-rust.github.io/ class=site-logo>Learning Rust</a></header><main><article><nav><button class=btn><i>⬅️</i> On this section</button>
33
<button class=btn>On this page <i>➡️</i></button></nav><header><h1>Lifetimes</h1><p></p></header><div id=article-body><p>When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.</p><p>Think,</p><ul><li>We have a <strong>variable binding</strong>, <code>a</code>.</li><li>We are <strong>referencing</strong> the value of <code>a</code>, <strong>from another variable binding</strong> <code>x</code>.
4-
We have to make sure that <strong><code>a</code> lives until we stop using <code>x</code></strong>.</li></ul><blockquote><p>🔎 <strong>Memory management</strong> is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of programming languages used <strong>Manual Memory Management</strong> which <strong>requires the programmer to give manual instructions</strong> to identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented <strong>Garbage collection</strong>(GC), a form of <strong>Automatic Memory Management</strong>(AMM). It determines what memory is no longer used and frees it automatically instead of relying on the programmer. However <strong>Objective-C and Swift</strong> provide similar functionality through <strong>Automatic Reference Counting</strong>(ARC).</p></blockquote><h2 id=what-is-lifetime>What is Lifetime?</h2><p>In Rust,</p><ul><li>A resource can only have <strong>one owner</strong> at a time. When it goes <strong>out of the scope</strong>, Rust removes it from the Memory.</li><li>When we want to reuse the same resource, we are <strong>referencing</strong> it/ <strong>borrowing</strong> its content.</li><li>When dealing with <strong>references</strong>, we have to specify <strong>lifetime annotations</strong> to provide instructions for the <strong>compiler</strong> to set <strong>how long</strong> those referenced resources <strong>should be alive</strong>.</li><li>⭐ But because of lifetime annotations make the <strong>code more verbose</strong>, in order to make <strong>common patterns</strong> more ergonomic, Rust allows lifetimes to be <strong>elided/omitted</strong> in <code>fn</code> definitions. In this case, the compiler assigns lifetime annotations <strong>implicitly</strong>.</li></ul><p>Lifetime annotations are <strong>checked at compile-time</strong>. Compiler checks when a data is used for the first and the last times. According to that, Rust manages memory in <strong>run time</strong>. This is the major reason for <strong>slower compilation times</strong> in Rust.</p><blockquote><ul><li>Unlike C and C++, <strong>usually</strong>, Rust doesn’t explicitly drop values at all.</li><li>Unlike GC, Rust doesn’t place deallocation calls where the data is no longer referenced.</li><li>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.</li></ul></blockquote><h2 id=usage>Usage</h2><p>Lifetimes are denoted with an apostrophe. By convention, a lowercase letter is used for naming. Usually <strong>starts with</strong> <code>'a</code> and <strong>follows alphabetic order</strong> when we need to add <strong>multiple lifetime</strong> annotations.</p><p>When using references,</p><h3 id=01-on-function-declaration>01. On Function Declaration</h3><ul><li><p>Input and output parameters with references should attach lifetimes after the <code>&</code> sign.
4+
We have to make sure that <strong><code>a</code> lives until we stop using <code>x</code></strong>.</li></ul><blockquote><p>🔎 <strong>Memory management</strong> is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of programming languages used <strong>Manual Memory Management</strong> which <strong>requires the programmer to give manual instructions</strong> to identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented <strong>Garbage collection</strong>(GC), a form of <strong>Automatic Memory Management</strong>(AMM). It determines what memory is no longer used and frees it automatically instead of relying on the programmer. However <strong>Objective-C and Swift</strong> provide similar functionality through <strong>Automatic Reference Counting</strong>(ARC).</p></blockquote><h2 id=what-is-lifetime>What is Lifetime?</h2><p>In Rust,</p><ul><li>A resource can only have <strong>one owner</strong> at a time. When it goes <strong>out of the scope</strong>, Rust removes it from the Memory.</li><li>When we want to reuse the same resource, we are <strong>referencing</strong> it/ <strong>borrowing</strong> its content.</li><li>When dealing with <strong>references</strong>, we have to specify <strong>lifetime annotations</strong> to provide instructions for the <strong>compiler</strong> to set <strong>how long</strong> those referenced resources <strong>should be alive</strong>.</li><li>⭐ But because of lifetime annotations make the <strong>code more verbose</strong>, in order to make <strong>common patterns</strong> more ergonomic, Rust allows lifetimes to be <strong>elided/omitted</strong> in <code>fn</code> definitions. In this case, the compiler assigns lifetime annotations <strong>implicitly</strong>.</li></ul><p>Lifetime annotations are <strong>checked at compile-time</strong>. The compiler checks when data is used for the first and the last times.</p><blockquote><ul><li>Unlike C and C++, <strong>usually</strong>, Rust doesn’t require explicitly dropping values at all.</li><li>Unlike GC, Rust doesn’t place deallocation calls where the data is no longer referenced.</li><li>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.</li></ul></blockquote><h2 id=usage>Usage</h2><p>Lifetimes are denoted with an apostrophe. By convention, a lowercase letter is used for naming. Usually <strong>starts with</strong> <code>'a</code> and <strong>follows alphabetic order</strong> when we need to add <strong>multiple lifetime</strong> annotations.</p><p>When using references,</p><h3 id=01-on-function-declaration>01. On Function Declaration</h3><ul><li><p>Input and output parameters with references should attach lifetimes after the <code>&</code> sign.
55
ex. <code>..(x: &'a str)</code> , <code>..(x: &'a mut str)</code></p></li><li><p>After the function name, we should mention that the given lifetimes are generic types.
66
ex. <code>fn foo&lt;'a>(..)</code> , <code>fn foo&lt;'a, 'b>(..)</code></p></li></ul><div class=highlight><pre tabindex=0 class=chroma><code class=language-rust data-lang=rust><span class=line><span class=cl><span class=c1>// No inputs, return a reference
77
</span></span></span><span class=line><span class=cl><span class=c1></span><span class=k>fn</span> <span class=nf>function</span><span class=o>&lt;</span><span class=na>&#39;a</span><span class=o>&gt;</span><span class=p>()</span><span class=w> </span>-&gt; <span class=kp>&amp;</span><span class=na>&#39;a</span> <span class=kt>str</span> <span class=p>{}</span><span class=w>
@@ -152,6 +152,6 @@
152152
</span></span></span><span class=line><span class=cl><span class=w>
153153
</span></span></span><span class=line><span class=cl><span class=w> </span><span class=fm>println!</span><span class=p>(</span><span class=s>&#34;Player: </span><span class=si>{}</span><span class=s>&#34;</span><span class=p>,</span><span class=w> </span><span class=n>player_fullname</span><span class=p>);</span><span class=w>
154154
</span></span></span><span class=line><span class=cl><span class=w></span><span class=p>}</span><span class=w>
155-
</span></span></span></code></pre></div></div><footer><time datetime=2022-10-17><i>🕒</i> Updated: 2022-10-17</time>
155+
</span></span></span></code></pre></div></div><footer><time datetime=2025-07-13><i>🕒</i> Updated: 2025-07-13</time>
156156
<a href=https://learning-rust.github.io/docs/borrowing/><i></i> Previous</a>
157157
<a href=https://learning-rust.github.io/docs/code-organization/>Next <i></i></a></footer></article><aside><div><button class=btn><i></i>Close</button></div><strong>On this page</strong><nav id=TableOfContents><ul><li><a href=#what-is-lifetime>What is Lifetime?</a></li><li><a href=#usage>Usage</a><ul><li><a href=#01-on-function-declaration>01. On Function Declaration</a></li><li><a href=#02-on-struct-or-enum-declaration>02. On Struct or Enum Declaration</a></li><li><a href=#03-with-impls-and-traits>03. With Impls and Traits</a></li><li><a href=#04-with-generic-types>04. With Generic Types</a></li></ul></li><li><a href=#lifetime-elision>Lifetime Elision</a></li><li><a href=#static-annotations><code>'static</code> Annotations</a></li><li><a href=#few-more-examples-about-the-usage-of-rust-lifetimes>Few more examples about the usage of Rust lifetimes.</a></li></ul></nav></aside></main><footer><div><i>🧑‍💻</i>Built by and copyright<a href=https://github.com/dumindu target=_blank>Dumindu Madunuwan</a><i>📅</i> 2016-2025<i>🚀</i> <a href=https://github.com/learning-rust target=_blank>GitHub</a></div><div><button class=btn><i>☀️</i><i></i><i>🌑</i></button></div></footer></div></div><div id=body-model-outer></div><script type=text/javascript src=/assets/js/docs.min.12ffdc25c0149ef34e761ee54587f2aae17affcb8375298ad2180851930cb142.js integrity></script></body></html>

0 commit comments

Comments
 (0)