Skip to content

Instantly share code, notes, and snippets.

@paulcadman
Last active August 29, 2015 14:22
Show Gist options
  • Save paulcadman/76a5881dd8e3715fc10f to your computer and use it in GitHub Desktop.
Save paulcadman/76a5881dd8e3715fc10f to your computer and use it in GitHub Desktop.
<title>Rust 1.0</title> <style> @import url(http://fonts.googleapis.com/css?family=Fira+Sans:400,600,400italic); @import url(https://fonts.googleapis.com/css?family=Fira+Mono:400,700,400italic); body { font-family: 'Fira Sans'; } h1, h2, h3 { font-family: 'Fira Sans'; font-weight: normal; } .remark-code, .remark-inline-code { font-family: 'Fira Mono'; } pre { font-family: 'Fira Mono'; } .small * { font-size: small !important } .large * { font-size: 105% !important } .smallpre * { font-size: 70% !important } </style> <textarea id="source">

class: center, middle

Rust 1.0


class: center, middle

Rust is a systems programming language that runs blazingly fast, prevents nearly all segfaults, and guarantees thread safety.

class: center, middle

Memory safety

Safe concurrency

Compile time checks

Manual memory management


Memory safety

#include<iostream>
#include<vector>
#include<string>

int main() {
    std::vector<std::string> v;
    v.push_back("Hello");
    std::string& x = v[0];
    v.push_back("world");
    std::cout << x;
}

Memory safety

#include<iostream>
#include<vector>
#include<string>

int main() {
    std::vector<std::string> v;
    v.push_back("Hello");
    std::string& x = v[0];
    v.push_back("world");
    std::cout << x;
}
$ g++ -Wall vector.cpp
$ ./a.out
Segmentation fault (core dumped)

Memory safety

fn main() {
    let mut v = vec![];
    v.push("Hello");
    let x = &v[0];
    v.push("world");
    println!("{}", x)
}

Memory safety

fn main() {
    let mut v = vec![];
    v.push("Hello");
    let x = &v[0];
    v.push("world");
    println!("{}", x)
}

.smallpre[

$ cargo run
   Compiling memorySafety v0.1.0 (file:///home/paul/talks/rust/memorySafety)
src/main.rs:5:5: 5:6 error: cannot borrow `v` as mutable because it is also
                            borrowed as immutable
src/main.rs:5     v.push("world");
                  ^
src/main.rs:4:14: 4:15 note: previous borrow of `v` occurs here; the immutable borrow
                             prevents subsequent moves or mutable borrows of `v` until
                             the borrow ends
src/main.rs:4     let x = &v[0];

]


Memory safety

fn main() {
    let mut v = vec![];
    v.push("Hello");
    let x = v[0].clone();
    v.push("world");
    println!("{}", x)
}

.smallpre[

$ cargo run
   Compiling memorySafety v0.1.0 (file:///home/paul/talks/rust/memorySafety)
     Running `target/debug/memorySafety`
Hello

]


Safe concurrency

#include<iostream>
#include<vector>
#include<thread>

void task(std::vector<int>* v) {
  for (int a = 1 ; a < 1000 ; a++) {
    v->push_back(a);
  }
}

int main() {
  std::vector<int>* v = new std::vector<int>();
  std::thread t1(task, v);
  std::thread t2(task, v);
  t1.join();
  t2.join();
  std::cout << "completed\n";
}

Safe concurrency

.medium[

$ g++ thread.cpp -std=c++0x -pthread -Wall
$ ./a.out
Segmentation fault (core dumped)
$ ./a.out
*** Error in `./a.out': double free or corruption (fasttop):
                        0x00007f99700008c0
$ ./a.out
*** Error in `*** Error in `./a.out': free():
                  invalid next size (fast): 0x00007f9fc00008c0 ***
$ ./a.out 
completed

]


Safe concurrency

use std::thread;

fn main() {
    let mut v = vec![];
    thread::spawn(move || {
        v.push(5);
    });
    v.push(3);
}

Safe concurrency

use std::thread;

fn main() {
    let mut v = vec![];
    thread::spawn(move || {
        v.push(5);
    });
    v.push(3);
}

.smallpre[

$ cargo run
   Compiling dataRaceRust v0.1.0 (file:///home/paul/talks/rust/dataRaceRust)
src/main.rs:8:5: 8:6 error: use of moved value: `v`
src/main.rs:8     v.push(3);
                  ^
src/main.rs:5:24: 7:6 note: `v` moved into closure environment here because
                     it has type `[closure(())]`, which is non-copyable
src/main.rs:5     thread::spawn(move || {
src/main.rs:6         v.push(5);
src/main.rs:7     });

]


Safe concurrency

use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
    let a = Arc::new(Mutex::new(vec![]));
    let a2 = a.clone();
    let child = thread::spawn(move || {
        let mut vec = a2.lock().unwrap();
        vec.push(5);
    });

    {
      let mut vec = a.lock().unwrap();
      vec.push(3);
    }
    
    child.join().unwrap();
    println!("{}", a.lock().unwrap().len());
}

Safe concurrency

use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
    let a = Arc::new(Mutex::new(vec![]));
    let a2 = a.clone();
    let child = thread::spawn(move || {
        let mut vec = a2.lock().unwrap();
        vec.push(5);
    });
    {
      let mut vec = a.lock().unwrap();
      vec.push(3);
    }
    child.join().unwrap();
    println!("{}", a.lock().unwrap().len());
}

.smallpre[

$ cargo run
   Compiling dataRaceRust v0.1.0 (file:///home/paul/talks/rust/dataRaceRust)
     Running `target/debug/dataRaceRust`
2

] </textarea> <script src="remark.min.js"> </script> <script> var slideshow = remark.create({ratio: "4:3"}); </script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment