Rust - Fernando Borretti

18
Rust Fernando Borretti

Transcript of Rust - Fernando Borretti

Page 1: Rust - Fernando Borretti

Rust

Fernando Borretti

Page 2: Rust - Fernando Borretti

What is it?

A new programming language from Mozilla.

I Memory safety without garbage collection.I Performance matching that of C.I Safe concurrency.

Page 3: Rust - Fernando Borretti

Without further ado

fn main() {println!("Hello, world!");

}

$ rustc hello.rs$ ./helloHello, world!

Page 4: Rust - Fernando Borretti

Memory Safety

I Rust constrains where and when you can dereference pointers.I The compiler can prove where to insert calls to free().I No dereferencing NULL pointers, no double free() errors, or buffer

overflows.

Page 5: Rust - Fernando Borretti

Example: Double Free

In C, this cuases a double free error:

#include <stdlib.h>

int main() {int* ptr = malloc(sizeof(int));int* ref = ptr;free(ptr);free(ref);

}

This is a contrived example, but double free errors happen often,e.g. freeing a recursive data structure.

Page 6: Rust - Fernando Borretti

Example: Double Free

In Rust:

fn main() {let ptr: Box<i32> = box 10;let r = ptr;

}

No explicit free(), the compiler figures out where to free the memory.

Page 7: Rust - Fernando Borretti

Ownership

Note, however, we can’t use both pointers at the same time. Thefollowing causes an error:

fn main() {let ptr: Box<i32> = box 10;let r = ptr;*ptr = 11;

}

Rust pointers are not raw numbers like in C, rather, they are likestd::unique_ptr or std::auto_ptr.

When pointer r is assigned the value of pointer ptr, ptr becomes invalidfor the rest of the lifetime of r.

Page 8: Rust - Fernando Borretti

Algebraic Data Types

ADTs are like classes, but in reverse.

Instead of having a base class, and classes that inherit from that one, etc.,you have a single type that can be any one of different variants.

Page 9: Rust - Fernando Borretti

Algebraic Data TypesInstead of this:

class Geometry {};

class Square : Geometry {double side;

public:Square(double s): side(s) {};

};

class Rectangle : Geometry {double length, width;

public:Rectangle(double l, double w): length(l), width(w) {};

};

int main() {Square sq = Square(10.0);Rectangle rect = Rectangle(1.2, 3.2);

}

Page 10: Rust - Fernando Borretti

Algebraic Data Types

You have this:

enum Geometry {Square(f64),Rectangle(f64, f64)

}

fn main() {let sq = Geometry::Square(10.0);let rect = Geometry::Rectangle(1.2, 3.2);

}

Page 11: Rust - Fernando Borretti

Option TypesDefined like this:

enum Option<T> {Some(T),None,

}

Used like this:

fn safe_division(dividend: int, divisor: int) -> Option<int> {if divisor == 0 {

None} else {

Some(dividend / divisor)}

}

Option types are essentially like having a NULL-able value, only unlike mosttype systems, you get to choose which types are nullable.

Page 12: Rust - Fernando Borretti

Pattern Matching

Like if isinstance(obj, type), but better.

Page 13: Rust - Fernando Borretti

Simple Matching

You can match values

fn print_int(x: i64) {match x {

1 => println!("one");2 => println!("two");_ => println!("Not implemented :C");

}}

And ranges and expressions:

match num {1 | 2 => ...;3 ... 5 => ...;_ => ...;

}

Page 14: Rust - Fernando Borretti

More Complex Matching

And other constructs, like the Option type:

let div = safe_division(1, 0);match div {

Some(q) => ...; // Do something with the quotientNone => ...; // Divisor was 0

}

Page 15: Rust - Fernando Borretti

Macros

Macros provide source-to-source transformation.

I You define a macro to match a particular pattern of source code, andtransform it into another.

I Patterns can include variables.

Page 16: Rust - Fernando Borretti

Example: MacrosFor example, let’s make a macro for constructing linked lists.

You have a list like this:

enum List {Cons(i64, Box<List>),Nil

}

So we write this recursive macro:

macro_rules! make_list (() => {

List::Nil};

($first:expr $(, $rest:expr)*) => {List::Cons($first, box make_list!($($rest),*));

};)

Page 17: Rust - Fernando Borretti

Example Source

So now, this:

let list = make_list!(1, 2, 3);

Expands to this:

let list =List::Cons(1,

box() List::Cons(2,box() List::Cons(3,

box() List::Nil)));

Page 18: Rust - Fernando Borretti

Current Status

Asymptotically approaching 1.0.