Rust
a low-level language with high-level abstractions
Hello, world!
$ cargo new --bin helloworld && cd helloworld
$ cargo run
Compiling helloworld v0.1.0
Running `target/debug/helloworld`
Hello, world!
$ cargo build --release
Compiling helloworld v0.1.0rust/helloworld
$ ./target/release/helloworld
Hello, world!
Low level
• C-like performances
• no VM
• no garbage collector
High level
fn is_odd(n: u32) -> bool {

n % 2 == 1

}



fn main() {

println!("Find the sum of all the squared odd numbers under 1000");

let upper = 1000;

let sum_of_squared_odd_numbers: u32 =

(0..).map(|n| n * n) // All natural numbers squared

.take_while(|&n| n < upper) // Below upper limit

.filter(|n| is_odd(*n)) // That are odd

.fold(0, |sum, i| sum + i); // Sum them

println!("result: {}", sum_of_squared_odd_numbers);

}
Gist
High level
• types
• type inference
• no manual memory management
• no null -> Option<T>
• no exception -> Result<T>
High level
• pattern matching
• closure
• immutable by default
• functional
Abstraction without
overhead
let mut acc = 0;

// Iterate: 0, 1, 2, ... to infinity

for n in 0.. {

let n_squared = n * n;



if n_squared >= upper {

// Break loop if exceeded the upper limit

break;

} else if is_odd(n_squared) {

acc += n_squared;

}

}

acc
let sum_of_squared_odd_numbers: u32 =

(0..).map(|n| n * n)

.take_while(|&n| n < upper)

.filter(|n| is_odd(*n))

.fold(0, |sum, i| sum + i);
Same perf!
(functional version even faster)
Not OO but Struct with impl
struct Person {

name: String,

}



impl Person {

pub fn say_hello(&self) {

println!("{} says hello!", self.name);

}

}



fn main() {

let p = Person { name: "Bibi".to_string() };

p.say_hello();

}
Gist
Typeclasses
#[derive(Debug)]

struct Age {

age: i32,

}



fn main() {

let age1 = Age { age: 12 };

let age2 = Age { age: 24 };

let sum = age1 + age2;

println!("age1 + age2 = {:?}", sum);

}
<anon>:9:15: 9:19 error: binary operation `+` cannot be applied to type `Age` [E0369]
<anon>:9 let sum = age1 + age2;
^~~~
Typeclassesuse std::ops::Add;



#[derive(Debug)]

struct Age {

age: i32,

}



impl Add for Age {

type Output = Age;



fn add(self, other: Age) -> Age {

println!("Adding!");

Age { age: self.age + other.age }

}

}



fn main() {

let age1 = Age { age: 12 };

let age2 = Age { age: 24 };

let sum = age1 + age2;

println!("age1 + age2 = {:?}", sum);

}
Gist
Program structure
• Cargo.toml
• unit tests next to implementation
• integration tests in /tests
Ecosystem
• cargo and crates.io
• dependency management
• build / cross-compilation
• https://play.rust-lang.org/
• very welcoming community
• https://github.com/kud1ing/awesome-rust
Documentation
• https://doc.rust-lang.org/book/ 

http://rustbyexample.com/
• https://this-week-in-rust.org/
Projects using Rust
• Servo
• Redox
• Firefox mp4
• Dropbox
But Go’s “memory footprint” was too
high for the massive storage systems
the company was trying to build.
Dropbox needed a language that
would take up less space in memory,
because so much memory would be
filled with all those files streaming
onto the machine. So, in the middle of
this two-and-half-year project, they
switched to Rust on the Diskotech
machines. And that’s what Dropbox is
now pushing into its data centers.
Web
• http://www.arewewebyet.org/
• Ex: https://tech.zalando.de/blog/getting-started-
with-rust/
Rust
• young language: 1.0 in May 2015
• very interesting
• first steps can be challenging
• compared to JVM: no GC / no JIT

Introduction to rust: a low-level language with high-level abstractions

  • 1.
    Rust a low-level languagewith high-level abstractions
  • 2.
    Hello, world! $ cargonew --bin helloworld && cd helloworld $ cargo run Compiling helloworld v0.1.0 Running `target/debug/helloworld` Hello, world!
  • 3.
    $ cargo build--release Compiling helloworld v0.1.0rust/helloworld $ ./target/release/helloworld Hello, world!
  • 4.
    Low level • C-likeperformances • no VM • no garbage collector
  • 5.
    High level fn is_odd(n:u32) -> bool {
 n % 2 == 1
 }
 
 fn main() {
 println!("Find the sum of all the squared odd numbers under 1000");
 let upper = 1000;
 let sum_of_squared_odd_numbers: u32 =
 (0..).map(|n| n * n) // All natural numbers squared
 .take_while(|&n| n < upper) // Below upper limit
 .filter(|n| is_odd(*n)) // That are odd
 .fold(0, |sum, i| sum + i); // Sum them
 println!("result: {}", sum_of_squared_odd_numbers);
 } Gist
  • 6.
    High level • types •type inference • no manual memory management • no null -> Option<T> • no exception -> Result<T>
  • 7.
    High level • patternmatching • closure • immutable by default • functional
  • 8.
    Abstraction without overhead let mutacc = 0;
 // Iterate: 0, 1, 2, ... to infinity
 for n in 0.. {
 let n_squared = n * n;
 
 if n_squared >= upper {
 // Break loop if exceeded the upper limit
 break;
 } else if is_odd(n_squared) {
 acc += n_squared;
 }
 }
 acc let sum_of_squared_odd_numbers: u32 =
 (0..).map(|n| n * n)
 .take_while(|&n| n < upper)
 .filter(|n| is_odd(*n))
 .fold(0, |sum, i| sum + i); Same perf! (functional version even faster)
  • 9.
    Not OO butStruct with impl struct Person {
 name: String,
 }
 
 impl Person {
 pub fn say_hello(&self) {
 println!("{} says hello!", self.name);
 }
 }
 
 fn main() {
 let p = Person { name: "Bibi".to_string() };
 p.say_hello();
 } Gist
  • 10.
    Typeclasses #[derive(Debug)]
 struct Age {
 age:i32,
 }
 
 fn main() {
 let age1 = Age { age: 12 };
 let age2 = Age { age: 24 };
 let sum = age1 + age2;
 println!("age1 + age2 = {:?}", sum);
 } <anon>:9:15: 9:19 error: binary operation `+` cannot be applied to type `Age` [E0369] <anon>:9 let sum = age1 + age2; ^~~~
  • 11.
    Typeclassesuse std::ops::Add;
 
 #[derive(Debug)]
 struct Age{
 age: i32,
 }
 
 impl Add for Age {
 type Output = Age;
 
 fn add(self, other: Age) -> Age {
 println!("Adding!");
 Age { age: self.age + other.age }
 }
 }
 
 fn main() {
 let age1 = Age { age: 12 };
 let age2 = Age { age: 24 };
 let sum = age1 + age2;
 println!("age1 + age2 = {:?}", sum);
 } Gist
  • 12.
    Program structure • Cargo.toml •unit tests next to implementation • integration tests in /tests
  • 13.
    Ecosystem • cargo andcrates.io • dependency management • build / cross-compilation • https://play.rust-lang.org/ • very welcoming community • https://github.com/kud1ing/awesome-rust
  • 14.
  • 15.
    Projects using Rust •Servo • Redox • Firefox mp4 • Dropbox But Go’s “memory footprint” was too high for the massive storage systems the company was trying to build. Dropbox needed a language that would take up less space in memory, because so much memory would be filled with all those files streaming onto the machine. So, in the middle of this two-and-half-year project, they switched to Rust on the Diskotech machines. And that’s what Dropbox is now pushing into its data centers.
  • 16.
    Web • http://www.arewewebyet.org/ • Ex:https://tech.zalando.de/blog/getting-started- with-rust/
  • 17.
    Rust • young language:1.0 in May 2015 • very interesting • first steps can be challenging • compared to JVM: no GC / no JIT