Date post: | 16-Apr-2017 |
Category: |
Technology |
Upload: | joao-oliveira |
View: | 128 times |
Download: | 0 times |
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
Why Rust?
Problems?Memory safety while being performant
Modern Concurrency
Tooling
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
Let's get started
Instalationeasiest way
curl -sSf https://static.rust-lang.org/rustup.sh | sh
Hello, Cargo!cargo new hello_world --bin
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;
Functionsfn print_number(x: i32) { println!("x is: {}", x); }
fn main() { print_number(4); }
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
List of di�erent numeric types
i8
i16
i32
i64
u8
u16
u32
u64
isize
usize
f32
f64
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 :("); }
IFlet x = 5; let y = if x == 5 { 10 } else { 15 }; // y: i32
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; } }
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); }
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`
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>
OwnershipHeap and stack
let x = Box::new(5); //heap let y = 42; //stack
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>
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>
Further ReadingEnums
Structs
Pattern Matching
channels
Generics
Traits
Macros
Thanks