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
62
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
51
Serverless
alocquet
0
25
Other Decks in Programming
See All in Programming
KubeCon NA 2024の全DB関連セッションを紹介
nnaka2992
0
110
return文におけるstd::moveについて
onihusube
1
1.4k
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
250
Package Traits
ikesyo
1
180
HTML/CSS超絶浅い説明
yuki0329
0
180
선언형 UI에서의 상태관리
l2hyunwoo
0
260
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
880
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
400
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
190
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
6
1.3k
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
140
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
140
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.5k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
Unsuck your backbone
ammeep
669
57k
Making Projects Easy
brettharned
116
6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
The Invisible Side of Design
smashingmag
299
50k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
GitHub's CSS Performance
jonrohan
1030
460k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Navigating Team Friction
lara
183
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
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