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
72
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
56
Serverless
alocquet
0
28
Other Decks in Programming
See All in Programming
Advanced Micro Frontends: Multi Version/ Framework Scenarios @WAD 2025, Berlin
manfredsteyer
PRO
0
440
階層化自動テストで開発に機動力を
ickx
1
410
MCPで実現できる、Webサービス利用体験について
syumai
7
1.9k
新メンバーも今日から大活躍!SREが支えるスケールし続ける組織のオンボーディング
honmarkhunt
5
9.3k
Rails Frontend Evolution: It Was a Setup All Along
skryukov
0
320
テスターからテストエンジニアへ ~新米テストエンジニアが歩んだ9ヶ月振り返り~
non0113
2
240
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
490
フロントエンドのパフォーマンスチューニング
koukimiura
6
2.3k
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
110
DMMを支える決済基盤の技術的負債にどう立ち向かうか / Addressing Technical Debt in Payment Infrastructure
yoshiyoshifujii
4
590
中級グラフィックス入門~効率的なメッシュレット描画~
projectasura
2
1.2k
iOS開発スターターキットの作り方
akidon0000
0
180
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
35
6.8k
Faster Mobile Websites
deanohume
308
31k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
760
A better future with KSS
kneath
238
17k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
How STYLIGHT went responsive
nonsquared
100
5.6k
How GitHub (no longer) Works
holman
314
140k
Automating Front-end Workflow
addyosmani
1370
200k
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