+ All Categories
Home > Technology > Introduction to Rust

Introduction to Rust

Date post: 16-Apr-2017
Category:
Upload: joao-oliveira
View: 128 times
Download: 0 times
Share this document with a friend
28
Introduction to Rust
Transcript
Page 1: Introduction to Rust

Introduction to Rust

Page 2: Introduction to Rust

Brief Introduction

First initiated by Graydon Hoare in 2006 as a part-timeproject

Adopted by Mozilla Foundation in ~2009

Reached 1.0 in May 2015

Currently on 1.12 (stable), 1.13 (beta) and 1.14(nightly)

Firefox is already shipping with Rust code since version 48

Page 4: Introduction to Rust

Why Rust?

Page 5: Introduction to Rust

Problems?Memory safety while being performant

Modern Concurrency

Tooling

Page 6: Introduction to Rust
Page 7: Introduction to Rust

Rust Features

move semantics

guaranteed memory safety

type inference

minimal runtime

zero-cost abstractions

fearless concurrency

trait-based generics

pattern matching

ef�cient C bindings

Page 8: Introduction to Rust

Let's get started

Page 9: Introduction to Rust

Instalationeasiest way

curl -sSf https://static.rust-lang.org/rustup.sh | sh

Page 10: Introduction to Rust

Hello Worldfn main() { println!("Hello, world!"); }

run example

Page 11: Introduction to Rust

Hello, Cargo!cargo new hello_world --bin

Page 12: Introduction to Rust

Variable bindingslet x = 5; // x: i32 let (x, y) = (1, 2); //type annotations let x: i32 = 5; let mut x = 5; // mut x: i32 x = 10;

Page 13: Introduction to Rust

Functionsfn print_number(x: i32) { println!("x is: {}", x); }

fn main() { print_number(4); }

Page 14: Introduction to Rust

Primitive TypesBooleans

let x = true;

let y: bool = false;

Charlet x = 'x'; let two_hearts = '�'

Numericlet x = 42; // x has type i32 let y = 1.0; // y has type f64

Page 15: Introduction to Rust

List of di�erent numeric types

Page 16: Introduction to Rust

i8

i16

i32

i64

u8

u16

u32

u64

isize

usize

f32

f64

Page 17: Introduction to Rust

IFlet x = 5; if x == 5 { println!("x is five!"); } else { println!("x is not five :("); } let x = 5;

if x == 5 { println!("x is five!"); } else if x == 6 { println!("x is six!"); } else { println!("x is not five or six :("); }

Page 18: Introduction to Rust

IFlet x = 5; let y = if x == 5 { 10 } else { 15 }; // y: i32

Page 19: Introduction to Rust

Loopsloop { println!("Loop forever!"); } let mut x = 5; // mut x: i32 let mut done = false; // mut done: bool

while !done { x += x - 3;

println!("{}", x);

if x % 5 == 0 { done = true; } }

Page 20: Introduction to Rust

LoopsRust does not have the “C-style” for loop on purpose.

Manually controlling each element of the loop is complicatedand error prone, even for experienced C developers.

for x in 0..10 { println!("{}", x); // x: i32}

for (index, value) in (5..10).enumerate() { //When you need to keep track of how many times you already looped println!("index = {} and value = {}", index, value); }

Page 21: Introduction to Rust

OwnershipVariable bindings have a property in Rust: they ‘have

ownership’ of what they’re bound to. This means that when abinding goes out of scope, Rust will free the bound resources.

For example:

let v = vec![1, 2, 3]; let v2 = v;

println!("v[0] is: {}", v[0]);//error: use of moved value: `v`

Page 22: Introduction to Rust

Ownershipfn take(v: Vec<i32>) { // what happens here isn’t important. }

let v = vec![1, 2, 3];

take(v);

println!("v[0] is: {}", v[0]);//same error, used of moved value `v`</i32>

Page 23: Introduction to Rust

OwnershipHeap and stack

let x = Box::new(5); //heap let y = 42; //stack

Page 24: Introduction to Rust

Ownershipborrowing

fn foo(v1: Vec<i32>, v2: Vec<i32>) -> (Vec<i32>, Vec<i32>, i32) { // do stuff with v1 and v2

// hand back ownership, and the result of our function (v1, v2, 42)}

let v1 = vec![1, 2, 3]; let v2 = vec![1, 2, 3];

let (v1, v2, answer) = foo(v1, v2);</i32></i32></i32></i32>

Page 25: Introduction to Rust

OwnershipReferences

n foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 { // do stuff with v1 and v2

// return the answer 42 }

let v1 = vec![1, 2, 3]; let v2 = vec![1, 2, 3];

let answer = foo(&v1, &v2);

// we can use v1 and v2 here!</i32></i32>

Page 26: Introduction to Rust

Further ReadingEnums

Structs

Pattern Matching

channels

Generics

Traits

Macros

Page 27: Introduction to Rust

ReferencesRust Book

Rust by example

Page 28: Introduction to Rust

Thanks


Recommended