SlideShare a Scribd company logo
Introduction to Rust
Brief Introduction
First initiated by Graydon Hoare in 2006 as a part-time
project
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
Who's using Rust
Why Rust?
Problems?
Memory safety while being performant
Modern Concurrency
Tooling
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
Let's get started
Instalation
easiest way
curl -sSf https://static.rust-lang.org/rustup.sh | sh
Hello World
fn main() {
println!("Hello, world!");
}
run example
Hello, Cargo!
cargo new hello_world --bin
Variable bindings
let x = 5; // x: i32
let (x, y) = (1, 2);
//type annotations
let x: i32 = 5;
let mut x = 5; // mut x: i32
x = 10;
Functions
fn print_number(x: i32) {
println!("x is: {}", x);
}
fn main() {
print_number(4);
}
Primitive Types
Booleans
let x = true;
let y: bool = false;
Char
let x = 'x';
let two_hearts = ' '
Numeric
let 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
IF
let 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 :(");
}
IF
let x = 5;
let y = if x == 5 { 10 } else { 15 }; // y: i32
Loops
loop {
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;
}
}
Loops
Rust does not have the “C-style” for loop on purpose.
Manually controlling each element of the loop is complicated
and 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);
}
Ownership
Variable bindings have a property in Rust: they ‘have
ownership’ of what they’re bound to. This means that when a
binding 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`
Ownership
fn 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>
Ownership
Heap and stack
let x = Box::new(5); //heap
let y = 42; //stack
Ownership
borrowing
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>
Ownership
References
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 Reading
Enums
Structs
Pattern Matching
channels
Generics
Traits
Macros
References
Rust Book
Rust by example
Thanks

More Related Content

Introduction to Rust