class: center, middle
class: center, middle
Rust is a systems programming language that runs blazingly fast, prevents nearly all segfaults, and guarantees thread safety.
class: center, middle
#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;
}
#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)
fn main() {
let mut v = vec![];
v.push("Hello");
let x = &v[0];
v.push("world");
println!("{}", x)
}
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];
]
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
]
#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";
}
.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
]
use std::thread;
fn main() {
let mut v = vec![];
thread::spawn(move || {
v.push(5);
});
v.push(3);
}
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 });
]
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());
}
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>