Skip to content

Commit d0d5db9

Browse files
committed
Merge remote-tracking branch 'origin' into docs
1 parent 3da7f69 commit d0d5db9

File tree

27 files changed

+116
-118
lines changed

27 files changed

+116
-118
lines changed

docs/docs/borrowing/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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>Borrowing · Learning Rust</title>
22
<link rel=canonical href=https://learning-rust.github.io/docs/borrowing/><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 class=active href=https://learning-rust.github.io/docs/borrowing/>Borrowing</a></li><li><a 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>
3-
<button class=btn>On this page <i>➡️</i></button></nav><header><h1>Borrowing</h1><p></p></header><div id=article-body><p>In real life applications, most of the times we have to pass variable bindings to other functions or assign them to other variable bindings. In this case, we are <strong>referencing</strong> the original binding; <strong>borrow</strong> the data of it.</p><h2 id=what-is-borrowing>What is Borrowing?</h2><blockquote><p><a href=https://github.com/nikomatsakis/rust-tutorials-keynote/blob/master/Ownership%20and%20Borrowing.pdf>Borrow (verb)</a><br>To receive something with the promise of returning it.</p></blockquote><h2 id=shared--mutable-borrowings>Shared & Mutable borrowings</h2><p>⭐️ There are two types of Borrowing,</p><ol><li><p><strong>Shared Borrowing</strong> <code>(&amp;T)</code></p><ul><li>A piece of data can be <strong>borrowed by a single or multiple users</strong>, but <strong>data should not be altered</strong>.</li></ul></li><li><p><strong>Mutable Borrowing</strong> <code>(&amp;mut T)</code></p><ul><li>A piece of data can be <strong>borrowed and altered by a single user</strong>, but the data should not be accessible for any other users at that time.</li></ul></li></ol><h2 id=rules-for-borrowings>Rules for borrowings</h2><p>There are very important rules regarding borrowing,</p><ol><li><p>One piece of data can be borrowed <strong>either</strong> as a shared borrow <strong>or</strong> as a mutable borrow <strong>at a given time. But not both at the same time</strong>.</p></li><li><p>Borrowing <strong>applies for both copy types and move types</strong>.</p></li><li><p>The concept of <strong>Liveness</strong></p></li></ol><div class=highlight><pre tabindex=0 class=chroma><code class=language-rust data-lang=rust><span class=line><span class=cl><span class=k>fn</span> <span class=nf>main</span><span class=p>()</span><span class=w> </span><span class=p>{</span><span class=w>
3+
<button class=btn>On this page <i>➡️</i></button></nav><header><h1>Borrowing</h1><p></p></header><div id=article-body><p>In real life applications, most of the times we have to pass variable bindings to other functions or assign them to other variable bindings. In this case, we are <strong>referencing</strong> the original binding; <strong>borrow</strong> the data of it.</p><h2 id=what-is-borrowing>What is Borrowing?</h2><blockquote><p><a href=https://github.com/nikomatsakis/rust-tutorials-keynote/blob/master/Ownership%20and%20Borrowing.pdf target=_blank>Borrow (verb)</a><br>To receive something with the promise of returning it.</p></blockquote><h2 id=shared--mutable-borrowings>Shared & Mutable borrowings</h2><p>⭐️ There are two types of Borrowing,</p><ol><li><p><strong>Shared Borrowing</strong> <code>(&amp;T)</code></p><ul><li>A piece of data can be <strong>borrowed by a single or multiple users</strong>, but <strong>data should not be altered</strong>.</li></ul></li><li><p><strong>Mutable Borrowing</strong> <code>(&amp;mut T)</code></p><ul><li>A piece of data can be <strong>borrowed and altered by a single user</strong>, but the data should not be accessible for any other users at that time.</li></ul></li></ol><h2 id=rules-for-borrowings>Rules for borrowings</h2><p>There are very important rules regarding borrowing,</p><ol><li><p>One piece of data can be borrowed <strong>either</strong> as a shared borrow <strong>or</strong> as a mutable borrow <strong>at a given time. But not both at the same time</strong>.</p></li><li><p>Borrowing <strong>applies for both copy types and move types</strong>.</p></li><li><p>The concept of <strong>Liveness</strong></p></li></ol><div class=highlight><pre tabindex=0 class=chroma><code class=language-rust data-lang=rust><span class=line><span class=cl><span class=k>fn</span> <span class=nf>main</span><span class=p>()</span><span class=w> </span><span class=p>{</span><span class=w>
44
</span></span></span><span class=line><span class=cl><span class=w> </span><span class=kd>let</span><span class=w> </span><span class=k>mut</span><span class=w> </span><span class=n>a</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=fm>vec!</span><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>,</span><span class=w> </span><span class=mi>3</span><span class=p>];</span><span class=w>
55
</span></span></span><span class=line><span class=cl><span class=w> </span><span class=kd>let</span><span class=w> </span><span class=n>b</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=o>&amp;</span><span class=k>mut</span><span class=w> </span><span class=n>a</span><span class=p>;</span><span class=w> </span><span class=c1>// &amp;mut borrow of `a` starts here
66
</span></span></span><span class=line><span class=cl><span class=c1></span><span class=w> </span><span class=c1>// ⁝

0 commit comments

Comments
 (0)