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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Arnaud
November 21, 2018
Programming
110
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
72
Serverless
alocquet
0
35
Other Decks in Programming
See All in Programming
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
210
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
150
Performance Engineering for Everyone
elenatanasoiu
0
250
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.9k
Lessons from Spec-Driven Development
simas
PRO
0
240
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
210
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
160
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
8.4k
はてなアカウント基盤 State of the Union
cockscomb
1
1.2k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
190
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
140
Webフレームワークの ベンチマークについて
yusukebe
0
190
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
0
380
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
Paper Plane
katiecoart
PRO
1
52k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
First, design no harm
axbom
PRO
2
1.2k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
480
Code Review Best Practice
trishagee
74
20k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
450
エンジニアに許された特別な時間の終わり
watany
107
250k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
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