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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Arnaud
November 21, 2018
Programming
100
2
Share
Rust - Ownership & Borrowing
Introduction to Rust ownership and borrowing concepts.
Arnaud
November 21, 2018
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
130
TLS 1.3 arrive!!! Et si on comprenait déjà ce que fait TLS 1.2 ?
alocquet
0
67
Serverless
alocquet
0
33
Other Decks in Programming
See All in Programming
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
1
480
AI時代だからこそ「Bloc」を採用する価値があるのかもしれない
takuroabe
0
230
AWSはOSSをどのように 考えているのか?
akihisaikeda
1
140
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
980
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
840
Transactional Change Stream Processing With Debezium and Apache Flink
gunnarmorling
1
130
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
3
590
デフォルト運用のCodeRabbit、1年で何が変わったか / How CodeRabbit Changed Our Code Review in 1 Year
bake0937
1
100
Agentic AI in the Frontend: Architectures with Open Standards @iJS London 2026
manfredsteyer
PRO
0
110
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
360
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
1.8k
iOS26時代の新規アプリ開発
yuukiw00w
0
200
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
The Curse of the Amulet
leimatthew05
1
12k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
570
Statistics for Hackers
jakevdp
799
230k
Six Lessons from altMBA
skipperchong
29
4.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.7k
Are puppies a ranking factor?
jonoalderson
1
3.4k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
150
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
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