an introduction to rust: the modern programming language to develop safe and efficient applications

93
An Introduction to Rust BIC Lazio Roma Casilina February 16, 2016

Upload: claudio-capobianco

Post on 20-Mar-2017

37 views

Category:

Software


2 download

TRANSCRIPT

Page 1: An introduction to Rust: the modern programming language to develop safe and efficient applications

An Introduction to RustBIC Lazio Roma Casilina

February 16, 2016

Page 3: An introduction to Rust: the modern programming language to develop safe and efficient applications

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.

Page 4: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 5: An introduction to Rust: the modern programming language to develop safe and efficient applications

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.

Page 6: An introduction to Rust: the modern programming language to develop safe and efficient applications

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!

Page 7: An introduction to Rust: the modern programming language to develop safe and efficient applications

Rust goalRuby, Javascript, Python, ...C C++ Java

Control Safety

Page 8: An introduction to Rust: the modern programming language to develop safe and efficient applications

Rust goal

Rust

Ruby, Javascript, Python, ...C C++ Java

Control Safety

Page 9: An introduction to Rust: the modern programming language to develop safe and efficient applications

Just to mention a few:

and many others, look at https://www.rust-lang.org/en-US/friends.html

RUST is already in production!

Page 10: An introduction to Rust: the modern programming language to develop safe and efficient applications

RUST applications

Few of applications written completely in Rust:

Servo, a web browser engine by Mozilla

Rocket, a web framework, focus on easy-to-use and fast

Redox, full fledged Operating System, 100% Rust

Habitat, application automation framework

and many others, look at https://github.com/kud1ing/awesome-rust

Page 11: An introduction to Rust: the modern programming language to develop safe and efficient applications

Hello world!

fn main() {

// Print text to the console

println!("Hello World!");

}

Page 12: An introduction to Rust: the modern programming language to develop safe and efficient applications

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.

Page 13: An introduction to Rust: the modern programming language to develop safe and efficient applications

What control means?void example() { vector<string> vector; … auto& elem = vector[0];}

data

length

capacity

elem

[0]

[1]

[2]

...

Stack Heap

vector

C++

string

Page 14: An introduction to Rust: the modern programming language to develop safe and efficient applications

What control means?

Ability to define abstractions that optimize away to nothing.

Page 15: An introduction to Rust: the modern programming language to develop safe and efficient applications

What control means?

vector data

length

Javacapacity

[0]

...

data capacity

‘H’

‘e’

...

Ability to define abstractions that optimize away to nothing.

Page 16: An introduction to Rust: the modern programming language to develop safe and efficient applications

What control means?

vector data

length

Javacapacity

[0]

...

data capacity

‘H’

‘e’

...

Ability to define abstractions that optimize away to nothing.

Page 17: An introduction to Rust: the modern programming language to develop safe and efficient applications

What safety means?void example() { vector<string> vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem;}

data

length

capacity

elem

[0]

vector

C++

Page 18: An introduction to Rust: the modern programming language to develop safe and efficient applications

What safety means?void example() { vector<string> vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem;}

data

length

capacity

elem

[0]

vector

C++

Page 19: An introduction to Rust: the modern programming language to develop safe and efficient applications

What safety means?void example() { vector<string> vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem;}

data

length

capacity

elem

[0]

vector

C++

[0]

[1]

Page 20: An introduction to Rust: the modern programming language to develop safe and efficient applications

What safety means?void example() { vector<string> vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem;}

data

length

capacity

elem

[0]

vector

C++

[0]

[1]

dangling pointer!

Page 21: An introduction to Rust: the modern programming language to develop safe and efficient applications

What safety means?void example() { vector<string> vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem;}

data

length

capacity

elem

[0]

vector

C++

[0]

[1]

aliasing

mutating

Page 22: An introduction to Rust: the modern programming language to develop safe and efficient applications

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.

Page 23: An introduction to Rust: the modern programming language to develop safe and efficient applications

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 =

Page 24: An introduction to Rust: the modern programming language to develop safe and efficient applications

What about the garbage collector?

With the garbage collector:

● we lose control● requires a runtime!

Anyway, it is insufficient to prevent data race or iterator invalidation.

Page 25: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 26: An introduction to Rust: the modern programming language to develop safe and efficient applications

Ownership

Page 27: An introduction to Rust: the modern programming language to develop safe and efficient applications

Ownership

Page 28: An introduction to Rust: the modern programming language to develop safe and efficient applications

Ownership

Page 29: An introduction to Rust: the modern programming language to develop safe and efficient applications

Ownership

Green doesn’t own the book anymore and cannot use it

Page 30: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownershipfn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec);}

data

length

capacity

vec

Page 31: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownershipfn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec);}

data

length

capacity

[0]

[1]vec

Page 32: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownershipfn 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

Page 33: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownershipfn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec);}

data

length

capacity

[0]

[1]vec

data

length

capacity

vec

Page 34: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownershipfn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec);}

data

length

capacity

veccannot be used because data is no longer available

Page 35: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn take(vec: Vec<i32>) {//…

}

Ownership - errorfn give() { let mut vec = Vec::new(); vec.push(1); vec.push(2); take(vec); vec.push(3);}

Page 36: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing

Page 37: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &T

one or more references to a resource

Page 38: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &T

read

read

read

&T

Page 39: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &T

Green can use its book again

Page 40: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &mut T

exactly one mutable reference

Page 41: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &mut T

Page 42: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &mut T&mut T

Page 43: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &mut T

&mut T

Page 44: An introduction to Rust: the modern programming language to develop safe and efficient applications

Borrowing with &mut T

Green can use its book again

Page 45: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn user(vec: &Vec<i32>) {//…

}

Borrowingfn lender() { let mut vec = Vec::new(); vec.push(1); vec.push(2); user(&vec);} data

length

capacity

vec

Page 46: An introduction to Rust: the modern programming language to develop safe and efficient applications

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);}

Page 47: An introduction to Rust: the modern programming language to develop safe and efficient applications

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);}

Page 48: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 49: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 50: An introduction to Rust: the modern programming language to develop safe and efficient applications

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);

}

Page 51: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetimes

Page 52: An introduction to Rust: the modern programming language to develop safe and efficient applications

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.

Page 53: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetimes

Page 54: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetimes&T

first borrowing

Page 55: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetimes

&T

second borrowing

!

Page 56: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetimes

&T

dangling pointer!

Page 57: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetime - first examplefn 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);}

Page 58: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetime - first examplefn 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);}

Page 59: An introduction to Rust: the modern programming language to develop safe and efficient applications

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 examplefirst borrowing

second borrowing(we return something that is not ours)

Page 60: An introduction to Rust: the modern programming language to develop safe and efficient applications

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 examplefirst 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

Page 61: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 62: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 63: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetime - example reviewedfn 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);}

Page 64: An introduction to Rust: the modern programming language to develop safe and efficient applications

Lifetime - example reviewedfn 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!

Page 65: An introduction to Rust: the modern programming language to develop safe and efficient applications

Other key concepts

Page 66: An introduction to Rust: the modern programming language to develop safe and efficient applications

Traits

“A trait is a language feature that tells the Rust compiler about functionality a type must provide.”

https://doc.rust-lang.org

Rust is really conservative about what a generic type can do!

Page 67: An introduction to Rust: the modern programming language to develop safe and efficient applications

Traitsfn is_equal<T>(a:T,b:T)->bool { a==b}

Page 68: An introduction to Rust: the modern programming language to develop safe and efficient applications

Traitsfn is_equal<T>(a:T,b:T)->bool { a==b}

generic type

Page 69: An introduction to Rust: the modern programming language to develop safe and efficient applications

Traitsfn is_equal<T>(a:T,b:T)->bool { a==b}

can T type be compared?we are not sure...

Page 70: An introduction to Rust: the modern programming language to develop safe and efficient applications

fn is_equal<T>(a:T,b:T)->bool { a==b}

Traitsdo not compile!

Page 71: An introduction to Rust: the modern programming language to develop safe and efficient applications

Traitsfn is_equal<T:PartialEq>(a:T,b:T)->bool { a==b}

type functionality is now explicit

OK!

Page 72: An introduction to Rust: the modern programming language to develop safe and efficient applications

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

Page 73: An introduction to Rust: the modern programming language to develop safe and efficient applications

No runtime

Unlike many other languages as Java, Go or JavaScript, Rust doesn’t have a runtime environment.

This make much easier to write a library in Rust and integrate it anywhere!

Page 74: An introduction to Rust: the modern programming language to develop safe and efficient applications

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;

Page 75: An introduction to Rust: the modern programming language to develop safe and efficient applications

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”);

Page 76: An introduction to Rust: the modern programming language to develop safe and efficient applications

Minimal core

Rust philosophy is to have a only few functionalities built-in in the language, and delegates a lot to libraries.

It also uses macro! as a clean way to reduce boilerplate code without “dirty” the language syntax.

According to me, this is one reason why it is loved

Page 77: An introduction to Rust: the modern programming language to develop safe and efficient applications

Ecosystem

Page 78: An introduction to Rust: the modern programming language to develop safe and efficient applications

Cargo

Rust’s package manager.

Manages dependencies and gives reproducible builds.

Cargo is one of the most powerful feature of Rust and it is the result of an awesome community!

Page 79: An introduction to Rust: the modern programming language to develop safe and efficient applications

Cargo

In Cargo, each library is called “crate”.

Stabilization pipeline for features is very quickly and nightly (as Rust language development itself).

“Stability without stagnation”

Page 80: An introduction to Rust: the modern programming language to develop safe and efficient applications

Crates

Can be either a binary or a library.

libc, types and bindings to native C functionsxml-rs, an XML library in pure Rusttime, utilities for working with time-related functionsserde, a generic serialization/deserialization framework

… and more like winapi, regex, url, rustc-serialize, etc.

Page 81: An introduction to Rust: the modern programming language to develop safe and efficient applications

Rustup is the Rust toolchain installer.

Easily switch between stable, beta, and nightly.

Cross-compiling is much simpler.

It is similar to Ruby's rbenv, Python's pyenv, or Node's nvm.

Install rustup bycurl https://sh.rustup.rs -sSf | sh

Rustup

Page 82: An introduction to Rust: the modern programming language to develop safe and efficient applications

Test

Rust objective is to ensure a high quality products.

Cargo has built-in a simple but efficient mechanism to write and run test

cargo test

Page 83: An introduction to Rust: the modern programming language to develop safe and efficient applications

Test#[test]fn it_works() { assert!(true)}

Page 84: An introduction to Rust: the modern programming language to develop safe and efficient applications

Test#[test]fn it_works() { assert!(false)}

Page 85: An introduction to Rust: the modern programming language to develop safe and efficient applications

Test#[test]#[should_panic]fn it_works() { assert!(false)}

Page 86: An introduction to Rust: the modern programming language to develop safe and efficient applications

Test can be also run on documented example!

Test

/// # Examples/// ``` /// use adder::add_two; /// assert_eq!(4, add_two(2)); /// ```

Page 87: An introduction to Rust: the modern programming language to develop safe and efficient applications

A full ecosystem

Formatter: rustfmt

Code completion: racer

Linter: clippy

Page 88: An introduction to Rust: the modern programming language to develop safe and efficient applications

Playground

https://play.rust-lang.org/

Page 89: An introduction to Rust: the modern programming language to develop safe and efficient applications

Community

Page 90: An introduction to Rust: the modern programming language to develop safe and efficient applications

Community

Rust has an active and amazing community.

https://this-week-in-rust.org/blog/2017/02/14/

Page 91: An introduction to Rust: the modern programming language to develop safe and efficient applications

Community

There are a lot of active channels, including:

forum, reddit, IRC, youtube, twitter, blog

And initiative like:

● crate of the week● rust weekly

Page 93: An introduction to Rust: the modern programming language to develop safe and efficient applications

This presentation is online!

Slidesharehttps://www.slideshare.net/wbigger

Google Drive (public, comments enabled) https://goo.gl/tKuHkI