SlideShare a Scribd company logo
1 of 59
Download to read offline
An Introduction to Rust
Rust Roma - Meetup
May 10, 2017
Who I am
Claudio Capobianco
wbigger@gmail.com
@settevolte
Meanwhile… install Rust!
https://www.rust-lang.org
curl https://sh.rustup.rs -sSf | sh
Code of this presentation is available on:
https://github.com/wbigger/RustIntro
When and why I did meet Rust
Last September, within my startup.
We were looking for a replacement for C/C++ code for our
multi-platform software library for IoT.
We considered Rust, Go, and Swift.
Why a replacement for C/C++?
Not only verbosity and safety, but also:
- test were not straightforward
- dependences management was a nightmare
- in general, continuous integration was really difficult to
obtain
More problems to tackle...
Much of time spent on debugging.
Several languages mixed in the same project.
Often modern languages are easy to learn but stucks
when software become complex.
What is Rust
Sponsored by Mozilla Research
The 0.1 release was in January 2012
The 1.0.0 stable release was in May 2015, since then the
backward compatibility is guaranteed
Most programming language by StackOverflow survey:
third place in 2015, first place in 2016!
Rust goal
Ruby,
Javascript,
Python, ...C C++ Java
Control Safety
Rust goal
Rust
Ruby,
Javascript,
Python, ...C C++ Java
Control Safety
Just to mention a few:
and many others, look at https://www.rust-lang.org/en-US/friends.html
RUST is already in production!
Hello world!
fn main() {
// Print text to the console
println!("Hello World!");
}
Key concepts
Rust is born with the aim to balance control and security.
That is, in other words:
operate at low level with high-level constructs.
What safety means?
Problem with safety happens when we have a resource
that at the same time:
● has alias: more references to the resource
● is mutable: someone can modify the resource
That is (almost) the definition of data race.
What safety means?
Problem with safety happens when we have a resource
that at the same time:
● has alias: more references to the resource
● is mutable: someone can modify the resource
That is (almost) the definition of data race.
alias + mutable =
The Rust Way
Rust solution to achieve both control and safety is to push
as much as possible checks at compile time.
This is achieved mainly through the concepts of
● Ownership
● Borrowing
● Lifetimes
Ownership
Ownership
Ownership
Ownership
Green doesn’t own
the book anymore
and cannot use it
fn take(vec: Vec<i32>) {
//…
}
Ownership
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
}
data
length
capacity
vec
fn take(vec: Vec<i32>) {
//…
}
Ownership
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
}
data
length
capacity
[0]
[1]
vec
fn take(vec: Vec<i32>) {
//…
}
Ownership
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
}
data
length
capacity
[0]
[1]
vec
data
length
capacity
vec
take ownership
fn take(vec: Vec<i32>) {
//…
}
Ownership
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
}
data
length
capacity
[0]
[1]
vec
data
length
capacity
vec
fn take(vec: Vec<i32>) {
//…
}
Ownership
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
}
data
length
capacity
vec
cannot be used
because data is
no longer
available
fn take(vec: Vec<i32>) {
//…
}
Ownership - error
fn give() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
take(vec);
vec.push(3);
}
Borrowing
Borrowing with &T
one or more references to a resource
Borrowing with &T
read
read
read
&T
Borrowing with &T
Green can use its
book again
Borrowing with &mut T
exactly one mutable reference
Borrowing with &mut T
Borrowing with &mut T
&mut T
Borrowing with &mut T
&mut T
Borrowing with &mut T
Green can use its
book again
fn user(vec: &Vec<i32>) {
//…
}
Borrowing
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
} data
length
capacity
vec
Borrowing
data
length
capacity
[0]
[1]
vec
fn user(vec: &Vec<i32>) {
//…
}
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
}
Borrowing
data
length
capacity
[0]
[1]
vec
datavec
fn user(vec: &Vec<i32>) {
//…
}
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
}
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
}
fn user(vec: &Vec<i32>) {
//…
}
Borrowing
data
length
capacity
[0]
[1]
vec
datavec
shared ref to vec
loan out vec
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
}
fn user(vec: &Vec<i32>) {
vec.push(3);
}
Borrowing
data
length
capacity
[0]
[1]
vec
datavec
what happens if I
try to modify it?
loan out vec
Borrowing - error
fn lender() {
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
user(&vec);
}
fn user(vec: &Vec<i32>) {
vec.push(3);
}
Lifetimes
Lifetimes
Remember that the owner has always the ability to destroy
(deallocate) a resource!
In simplest cases, compiler recognize the problem and
refuse to compile.
In more complex scenarios compiler needs an hint.
Lifetimes
Lifetimes
&T
first borrowing
Lifetimes
&T
second
borrowing
!
Lifetimes
&T
dangling pointer!
Lifetime - first example
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - first example
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - first example
first borrowing
second borrowing
(we return something that is
not ours)
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - first example
first borrowing
second borrowing
(we return something that is
not ours)
we know that “s2” is valid as long
as “line” is valid, but compiler
doesn’t know
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - first example
refuse to
compile
fn skip_prefix(line: &str, prefix: &str) -> &str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - first example
Lifetime - example reviewed
fn skip_prefix<'a>(line: &'a str, prefix: &str) -> &'a str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
Lifetime - example reviewed
fn skip_prefix<'a>(line: &'a str, prefix: &str) -> &'a str {
let (s1,s2) = line.split_at(prefix.len());
s2
}
fn print_hello() {
let line = "lang:en=Hello World!";
let v;
{
let p = "lang:en=";
v = skip_prefix(line, p);
}
println!("{}", v);
}
borrowing source is now
explicit, through the
lifetime parameter
Hello World!
Other key concepts
Lock data not code
The technique to lock data instead of code is widely used,
but generally is up to responsibility of developers.
“Lock data, not code” is enforced in Rust
Option type
null does not exist in Rust
Rust uses the Option type instead.
enum Option<T> {
None,
Some(T),
}
let x = Some(7);
let y = None;
Result type
exceptions do not exist in Rust
Rust uses Result type instead.
enum Result<T, E> {
Ok(T),
Err(E),
}
let x = Ok(7);
let y = Error(“Too bad”);
Credits
Youtube
The Rust Programming Language by Alex Crichton
SpeakerDeck
Hello, Rust! — An overview by Ivan Enderlin

More Related Content

What's hot

Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)nikomatsakis
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22nikomatsakis
 
Rust Mozlando Tutorial
Rust Mozlando TutorialRust Mozlando Tutorial
Rust Mozlando Tutorialnikomatsakis
 
Rust: Reach Further
Rust: Reach FurtherRust: Reach Further
Rust: Reach Furthernikomatsakis
 
Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2VMware Tanzu
 
Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics Prakash Pimpale
 
(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your GroovyAlonso Torres
 
Using QString effectively
Using QString effectivelyUsing QString effectively
Using QString effectivelyRoman Okolovich
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threadsShuo Chen
 
Killing Bugs with Pry
Killing Bugs with PryKilling Bugs with Pry
Killing Bugs with PryJason Carter
 
Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)Daniel Lemire
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012aleks-f
 
DASP Top10 for OWASP Thailand Chapter by s111s
DASP Top10 for OWASP Thailand Chapter by s111s DASP Top10 for OWASP Thailand Chapter by s111s
DASP Top10 for OWASP Thailand Chapter by s111s s111s object
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Stefan Marr
 
OpenCog Developer Workshop
OpenCog Developer WorkshopOpenCog Developer Workshop
OpenCog Developer WorkshopIbby Benali
 

What's hot (20)

Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)Rust: Reach Further (from QCon Sao Paolo 2018)
Rust: Reach Further (from QCon Sao Paolo 2018)
 
Rust-lang
Rust-langRust-lang
Rust-lang
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22
 
Rust Mozlando Tutorial
Rust Mozlando TutorialRust Mozlando Tutorial
Rust Mozlando Tutorial
 
Rust: Reach Further
Rust: Reach FurtherRust: Reach Further
Rust: Reach Further
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 
Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2Leap Ahead with Redis 6.2
Leap Ahead with Redis 6.2
 
Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics Natural Language Toolkit (NLTK), Basics
Natural Language Toolkit (NLTK), Basics
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
 
(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy(Greach 2015) Dsl'ing your Groovy
(Greach 2015) Dsl'ing your Groovy
 
Using QString effectively
Using QString effectivelyUsing QString effectively
Using QString effectively
 
Return of c++
Return of c++Return of c++
Return of c++
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
 
Killing Bugs with Pry
Killing Bugs with PryKilling Bugs with Pry
Killing Bugs with Pry
 
Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)Engineering fast indexes (Deepdive)
Engineering fast indexes (Deepdive)
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012
 
DASP Top10 for OWASP Thailand Chapter by s111s
DASP Top10 for OWASP Thailand Chapter by s111s DASP Top10 for OWASP Thailand Chapter by s111s
DASP Top10 for OWASP Thailand Chapter by s111s
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
 
OpenCog Developer Workshop
OpenCog Developer WorkshopOpenCog Developer Workshop
OpenCog Developer Workshop
 

Similar to Rust Intro @ Roma Rust meetup

Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devicesLars Gregori
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 pramode_ce
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.Mike Brevoort
 
Real-Time Web Programming with PrismTech Vortex Web
Real-Time Web Programming with PrismTech Vortex WebReal-Time Web Programming with PrismTech Vortex Web
Real-Time Web Programming with PrismTech Vortex WebADLINK Technology IoT
 
Building Real-Time Web Applications with Vortex-Web
Building Real-Time Web Applications with Vortex-WebBuilding Real-Time Web Applications with Vortex-Web
Building Real-Time Web Applications with Vortex-WebAngelo Corsaro
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevFelix Geisendörfer
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkHendy Irawan
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)Felix Geisendörfer
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018Codemotion
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Get Ahead with HTML5 on Moible
Get Ahead with HTML5 on MoibleGet Ahead with HTML5 on Moible
Get Ahead with HTML5 on Moiblemarkuskobler
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersRutenis Turcinas
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the wayOleg Podsechin
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...apidays
 

Similar to Rust Intro @ Roma Rust meetup (20)

Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devices
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Real-Time Web Programming with PrismTech Vortex Web
Real-Time Web Programming with PrismTech Vortex WebReal-Time Web Programming with PrismTech Vortex Web
Real-Time Web Programming with PrismTech Vortex Web
 
Building Real-Time Web Applications with Vortex-Web
Building Real-Time Web Applications with Vortex-WebBuilding Real-Time Web Applications with Vortex-Web
Building Real-Time Web Applications with Vortex-Web
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian Network
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018
Tomas Della Vedova - Building a future proof framework - Codemotion Milan 2018
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Get Ahead with HTML5 on Moible
Get Ahead with HTML5 on MoibleGet Ahead with HTML5 on Moible
Get Ahead with HTML5 on Moible
 
Devoxx 17 - Swift server-side
Devoxx 17 - Swift server-sideDevoxx 17 - Swift server-side
Devoxx 17 - Swift server-side
 
Introduction to Rust
Introduction to RustIntroduction to Rust
Introduction to Rust
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
C++ references
C++ referencesC++ references
C++ references
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

Rust Intro @ Roma Rust meetup

  • 1. An Introduction to Rust Rust Roma - Meetup May 10, 2017
  • 2. Who I am Claudio Capobianco wbigger@gmail.com @settevolte
  • 3. Meanwhile… install Rust! https://www.rust-lang.org curl https://sh.rustup.rs -sSf | sh Code of this presentation is available on: https://github.com/wbigger/RustIntro
  • 4. When and why I did meet Rust Last September, within my startup. We were looking for a replacement for C/C++ code for our multi-platform software library for IoT. We considered Rust, Go, and Swift.
  • 5. Why a replacement for C/C++? Not only verbosity and safety, but also: - test were not straightforward - dependences management was a nightmare - in general, continuous integration was really difficult to obtain
  • 6. More problems to tackle... Much of time spent on debugging. Several languages mixed in the same project. Often modern languages are easy to learn but stucks when software become complex.
  • 7. What is Rust Sponsored by Mozilla Research The 0.1 release was in January 2012 The 1.0.0 stable release was in May 2015, since then the backward compatibility is guaranteed Most programming language by StackOverflow survey: third place in 2015, first place in 2016!
  • 10. Just to mention a few: and many others, look at https://www.rust-lang.org/en-US/friends.html RUST is already in production!
  • 11. Hello world! fn main() { // Print text to the console println!("Hello World!"); }
  • 12. Key concepts Rust is born with the aim to balance control and security. That is, in other words: operate at low level with high-level constructs.
  • 13. What safety means? Problem with safety happens when we have a resource that at the same time: ● has alias: more references to the resource ● is mutable: someone can modify the resource That is (almost) the definition of data race.
  • 14. What safety means? Problem with safety happens when we have a resource that at the same time: ● has alias: more references to the resource ● is mutable: someone can modify the resource That is (almost) the definition of data race. alias + mutable =
  • 15. The Rust Way Rust solution to achieve both control and safety is to push as much as possible checks at compile time. This is achieved mainly through the concepts of ● Ownership ● Borrowing ● Lifetimes
  • 19. Ownership Green doesn’t own the book anymore and cannot use it
  • 20. fn take(vec: Vec<i32>) { //… } Ownership fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); } data length capacity vec
  • 21. fn take(vec: Vec<i32>) { //… } Ownership fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); } data length capacity [0] [1] vec
  • 22. fn take(vec: Vec<i32>) { //… } Ownership fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); } data length capacity [0] [1] vec data length capacity vec take ownership
  • 23. fn take(vec: Vec<i32>) { //… } Ownership fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); } data length capacity [0] [1] vec data length capacity vec
  • 24. fn take(vec: Vec<i32>) { //… } Ownership fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); } data length capacity vec cannot be used because data is no longer available
  • 25. fn take(vec: Vec<i32>) { //… } Ownership - error fn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); vec.push(3); }
  • 27. Borrowing with &T one or more references to a resource
  • 29. Borrowing with &T Green can use its book again
  • 30. Borrowing with &mut T exactly one mutable reference
  • 34. Borrowing with &mut T Green can use its book again
  • 35. fn user(vec: &Vec<i32>) { //… } Borrowing fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); } data length capacity vec
  • 36. Borrowing data length capacity [0] [1] vec fn user(vec: &Vec<i32>) { //… } fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); }
  • 37. Borrowing data length capacity [0] [1] vec datavec fn user(vec: &Vec<i32>) { //… } fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); }
  • 38. fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); } fn user(vec: &Vec<i32>) { //… } Borrowing data length capacity [0] [1] vec datavec shared ref to vec loan out vec
  • 39. fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); } fn user(vec: &Vec<i32>) { vec.push(3); } Borrowing data length capacity [0] [1] vec datavec what happens if I try to modify it? loan out vec
  • 40. Borrowing - error fn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec); } fn user(vec: &Vec<i32>) { vec.push(3); }
  • 42. Lifetimes Remember that the owner has always the ability to destroy (deallocate) a resource! In simplest cases, compiler recognize the problem and refuse to compile. In more complex scenarios compiler needs an hint.
  • 47. Lifetime - first example fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); }
  • 48. Lifetime - first example fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); }
  • 49. fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); } Lifetime - first example first borrowing second borrowing (we return something that is not ours)
  • 50. fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); } Lifetime - first example first borrowing second borrowing (we return something that is not ours) we know that “s2” is valid as long as “line” is valid, but compiler doesn’t know
  • 51. fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); } Lifetime - first example refuse to compile
  • 52. fn skip_prefix(line: &str, prefix: &str) -> &str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); } Lifetime - first example
  • 53. Lifetime - example reviewed fn skip_prefix<'a>(line: &'a str, prefix: &str) -> &'a str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); }
  • 54. Lifetime - example reviewed fn skip_prefix<'a>(line: &'a str, prefix: &str) -> &'a str { let (s1,s2) = line.split_at(prefix.len()); s2 } fn print_hello() { let line = "lang:en=Hello World!"; let v; { let p = "lang:en="; v = skip_prefix(line, p); } println!("{}", v); } borrowing source is now explicit, through the lifetime parameter Hello World!
  • 56. Lock data not code The technique to lock data instead of code is widely used, but generally is up to responsibility of developers. “Lock data, not code” is enforced in Rust
  • 57. Option type null does not exist in Rust Rust uses the Option type instead. enum Option<T> { None, Some(T), } let x = Some(7); let y = None;
  • 58. Result type exceptions do not exist in Rust Rust uses Result type instead. enum Result<T, E> { Ok(T), Err(E), } let x = Ok(7); let y = Error(“Too bad”);
  • 59. Credits Youtube The Rust Programming Language by Alex Crichton SpeakerDeck Hello, Rust! — An overview by Ivan Enderlin