Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rust - Ownership & Borrowing
Search
Arnaud
November 21, 2018
Programming
2
77
Rust - Ownership & Borrowing
Introduction to Rust ownership and borrowing concepts.
Arnaud
November 21, 2018
Tweet
Share
More Decks by Arnaud
See All by Arnaud
TLS 1.3 arrive!!! Et si on comprenait déjà ce que fait TLS 1.2 ?
alocquet
1
120
TLS 1.3 arrive!!! Et si on comprenait déjà ce que fait TLS 1.2 ?
alocquet
0
58
Serverless
alocquet
0
29
Other Decks in Programming
See All in Programming
AIコーディングエージェント(Manus)
kondai24
0
220
JETLS.jl ─ A New Language Server for Julia
abap34
2
460
AIコーディングエージェント(Gemini)
kondai24
0
280
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
Navigating Dependency Injection with Metro
l2hyunwoo
1
190
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
460
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
470
Developing static sites with Ruby
okuramasafumi
0
330
GoLab2025 Recap
kuro_kurorrr
0
780
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
290
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
4
1.3k
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
18
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Utilizing Notion as your number one productivity tool
mfonobong
2
190
Test your architecture with Archunit
thirion
1
2.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
2
66
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
RailsConf 2023
tenderlove
30
1.3k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
120
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
2.8k
Accessibility Awareness
sabderemane
0
24
Transcript
Rust Rust Onwership & Onwership & Borrowing Borrowing
Stack vs Heap Stack vs Heap Stack Stack fast automatically
desaclocated at scope end limited size pointers and primitives storage
Heap Heap slower manage memory allocation
Control vs Security Control vs Security Control : optimisation Security
: avoid memory allocation problems / memory leaks
Examples Examples allocating memory and never free it dangling pointers
keep a reference to freed memory have a reference to a moved variable (realloc) other problems...
Examples in C++ Examples in C++
Examples in C++ Examples in C++
Examples in C++ Examples in C++
Rust Rust manage desallocation at compile time avoid alias &
mutability at same time
Rust Rust Ownership Ownership
What happens when What happens when variable is copied variable
is copied primitives : stack copy others : "move" String example copy stack representation : pointer, length, capacity disable rst variable -> 'value used here after move'
Take Ownership Take Ownership copy variable into another variable pass
variable to function return variable from function
Keep Ownership after Keep Ownership after function call function call
send inputs as outputs
Clone Clone deep copy : stack + heap never done
automaticaly by rust done automaticaly by rust for Stack only data (@see Copy trait) : integers, boolean, oat, character, tuple of these types
Borrowing Borrowing Borrow a variable Keep ownership
: Reference : Reference
Borrowing : 2 ways Borrowing : 2 ways fn(val: &String)
no mutability any borrow as you want fn(val: &mut String) mutable other constraints
Mutable Reference Mutable Reference cannot make a mutable reference if
an immutable reference has been done
Mutable Reference Mutable Reference cannot make multiple mutables references avoid
data race
String case String case String : stored in the heap
&String : reference to a String &str : slice from a String &'static str : slice : stored in program
String case : &str String case : &str
world name value ptr len 5 index value 0 h
1 e 2 l 3 l 4 o 5 6 w 7 o 8 r 9 l 10 d s name value ptr len 11 capacity 11
String case String case as function parameter ? If we
don't need ownership -> &str fn example(value : &str) { println!(value); }
String case String case as function parameter ? If we
need owership -> String fn example($self, value : String) { self.value = value; }
String case String case &str : if the variable must
be used outside my struct/fn &str : if the variable should be large : avoid deep copy
Useful traits Useful traits Box : store data on the
heap Deref : &String -> &str Rc : data owned by multiple variable Mutex Into Cow
References References 2014 : Memory Safety in Rust : Intro
: String vs &str : https://www.youtube.com/watch? v=WQbg6ZMQJvQ http://intorust.com/ https://hermanradtke.com/2015/05/03/string-vs- str-in-rust-functions.html