BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and...

134
BEAM + Rust BEAM + Rust A Match Made in Heaven

Transcript of BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and...

Page 1: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

BEAM + RustBEAM + RustA Match Made in Heaven

1 / 79

Page 2: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sonny ScrogginSonny Scroggin

scrogson

2 / 79

Page 3: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

3 / 79

Page 4: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

BEAM + RustBEAM + RustA Match Made in Heaven

4 / 79

Page 5: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

What is Rust?Why Rust?How to Rust?BEAM + Rust

5 / 79

Page 6: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

What isWhat isRust?Rust?

6 / 79

Page 7: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Rust is a systems programminglanguage.

7 / 79

Page 8: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Empowering everyone to buildreliable and ef�cient software.

8 / 79

Page 9: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

When it comes to systems programmingWhen it comes to systems programming

9 / 79

Page 10: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Meh, I already know C/C++Meh, I already know C/C++

10 / 79

Page 11: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Why Rust?Why Rust?11 / 79

Page 12: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

PerformancePerformanceRust is blazingly fast and memory-efficient: with nono

runtimeruntime or garbage collectorgarbage collector, it can power performance-critical services, run on embedded devices, and easily

integrate with other languagesintegrate with other languages.

12 / 79

Page 13: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Safe Rust is generally fast enough without even trying.

13 / 79

Page 14: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ReliabilityReliabilityRust’s rich type systemtype system and ownershipownership model guaranteememory-safetymemory-safety and thread-safetythread-safety — and enable you to

eliminate many classes of bugs at compile-time.

14 / 79

Page 15: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ProductivityProductivityRust has great documentationdocumentation, a friendly compilerfriendly compiler with

useful error messages, and top-notch toolingtop-notch tooling.

15 / 79

Page 16: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Rust was initially created to solveRust was initially created to solvetwo difficult problemstwo difficult problems

How do you make systems programming safe?How do you make concurrency painless?

16 / 79

Page 17: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Memory-safetyMemory-safety

17 / 79

Page 18: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Memory-safety is a term used to describe applicationsthat access the operating system's memory in a way that

doesn't cause errors.

18 / 79

Page 19: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Common memory-safety issuesCommon memory-safety issuesBuffer over�owsRace conditionsNull pointersStack/Heap exhaustion/corruptionUse after free / double free

Memory-safety issues have been hovering at 70% for the past 12 years.

19 / 79

Page 20: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Thread-safetyThread-safety

20 / 79

Page 21: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Thread-safe code only manipulates shared data structuresin a manner that ensures that all threads behave properlyand fulfill their design specifications without unintended

interaction.

21 / 79

Page 22: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Memory and concurrency bugs o�en come down to codeaccessing data when it shouldn't.

22 / 79

Page 23: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Linus TorvaldsLinus TorvaldsBut I write bug-free C

code, why should Ibother?

23 / 79

Page 24: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Linux Kernel VulnerabilitiesLinux Kernel Vulnerabilities

24 / 79

Page 25: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Microso�: 70 percent of all securityMicroso�: 70 percent of all securitybugs are memory safety issuesbugs are memory safety issues

https://www.zdnet.com/article/microsoft-70-percent-of-all-security-bugs-are-memory-safety-issues

25 / 79

Page 26: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

OwnershipOwnership

26 / 79

Page 27: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Every value has anEvery value has an"owning scope""owning scope"

27 / 79

Page 28: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope vec.push(0); vec.push(1); // scope ends, `vec` is destroyed}

28 / 79

Page 29: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Create a new vector

let mut vec = Vec::new(); // owned by new_vec's scopefn new_vec() {

vec.push(0); vec.push(1); // scope ends, `vec` is destroyed}

28 / 79

Page 30: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Push some elements

vec.push(0); vec.push(1);

fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope

// scope ends, `vec` is destroyed}

28 / 79

Page 31: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

vec is now dropped

// scope ends, `vec` is destroyed

fn new_vec() { let mut vec = Vec::new(); // owned by new_vec's scope vec.push(0); vec.push(1);

}

28 / 79

Page 32: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

29 / 79

Page 33: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn new_vec() -> Vec<i32> { let mut vec = Vec::new(); vec.push(0); vec.push(1); vec} fn print_vec(vec: Vec<i32>) {

for i in vec iter() {

29 / 79

Page 34: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Create a new vec

let mut vec = Vec::new();fn new_vec() -> Vec<i32> {

vec.push(0); vec.push(1); vec} fn print_vec(vec: Vec<i32>) {

for i in vec iter() {

29 / 79

Page 35: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Push some integers

vec.push(0); vec.push(1);

fn new_vec() -> Vec<i32> { let mut vec = Vec::new();

vec} fn print_vec(vec: Vec<i32>) {

for i in vec iter() {

29 / 79

Page 36: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Return the vec

vec

fn new_vec() -> Vec<i32> { let mut vec = Vec::new(); vec.push(0); vec.push(1);

} fn print_vec(vec: Vec<i32>) {

for i in vec.iter() {

29 / 79

Page 37: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn print_vec(vec: Vec<i32>) { for i in vec.iter() { println!("{}", i) }}

 fn use vec() {

29 / 79

Page 38: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

print_vec takes ownership of vec

fn print_vec(vec: Vec<i32>) {

}

for i in vec.iter() { println!("{}", i) }

 

29 / 79

Page 39: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Interate over each entry in the vec

for i in vec.iter() { println!("{}", i) }

} fn print_vec(vec: Vec<i32>) {

} fn use vec() {

29 / 79

Page 40: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

vec gets deallocated here

}

 fn print_vec(vec: Vec<i32>) { for i in vec.iter() { println!("{}", i)

} fn use_vec() {

let vec = new vec();

29 / 79

Page 41: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn use_vec() { let vec = new_vec(); print_vec(vec);}

for i in vec.iter() { println!("{}", i) }} 

29 / 79

Page 42: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Take ownership of vec

let vec = new_vec();

for i in vec.iter() { println!("{}", i) }

} fn use_vec() {

print_vec(vec);}

29 / 79

Page 43: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Pass ownership to print_vec

print_vec(vec);

for i in vec.iter() { println!("{}", i) }} fn use_vec() { let vec = new_vec();

}

29 / 79

Page 44: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn use_vec() { let vec = new_vec(); print_vec(vec);  for i in vec.iter() { println!("{}", i * 2) }}

30 / 79

Page 45: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Continue using vec

for i in vec.iter() { println!("{}", i * 2) }

fn use_vec() { let vec = new_vec(); print_vec(vec); 

}

30 / 79

Page 46: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

error: use of moved value: `vec` for i in vec.iter() { ^~~

31 / 79

Page 47: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

How can we prevent vec from being dropped at the endof print_vec?

32 / 79

Page 48: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

BorrowingBorrowing

33 / 79

Page 49: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

&&To borrow a value, you make a reference to it

34 / 79

Page 50: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn print_vec(vec: &Vec<i32>) { // `vec` is borrowed for this scope  for i in vec.iter() { println!("{}", i) }  // the borrow ends}

35 / 79

Page 51: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn use_vec() { let vec = make_vec(); print_vec(&vec);  for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}

36 / 79

Page 52: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Take ownership of vec

let vec = make_vec();fn use_vec() {

print_vec(&vec);  for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}

36 / 79

Page 53: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Lend vec to print_vec

print_vec(&vec);

fn use_vec() { let vec = make_vec();

  for i in vec.iter() { println!("{}", i * 2) } // vec is destroyed here}

36 / 79

Page 54: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Continue using vec

for i in vec.iter() { println!("{}", i * 2) }

fn use_vec() { let vec = make_vec(); print_vec(&vec); 

// vec is destroyed here}

36 / 79

Page 55: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

// vec is destroyed here

fn use_vec() { let vec = make_vec(); print_vec(&vec);  for i in vec.iter() { println!("{}", i * 2) }

}

36 / 79

Page 56: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

References come in twoReferences come in twoflavorsflavors

37 / 79

Page 57: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

&T&TImmutable references

38 / 79

Page 58: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

&mut T&mut TMutable references

39 / 79

Page 59: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Borrowing has no runtimeBorrowing has no runtimeoverheadoverhead

Rust checks these rules at compile time

40 / 79

Page 60: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

fn push_all(from: &Vec<i32>, to: &mut Vec<i32>) { for i in from.iter() { to.push(*i); }}

41 / 79

Page 61: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

push_all(&vec, &mut vec)push_all(&vec, &mut vec)

42 / 79

Page 62: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

error: cannot borrow `vec` as mutable because it is also borrowed as immutablepush_all(&vec, &mut vec); ^~~

43 / 79

Page 63: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

For memory-safety, this means you can program withouta garbage collector and without fear of segfaults, because

Rust will catch your mistakes.

44 / 79

Page 64: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ConcurrencyConcurrency

45 / 79

Page 65: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ChannelsChannelsTransfers ownership of the messages sent along itSend a pointer from one thread to another withoutfear of race conditions.Enforce thread isolation.

46 / 79

Page 66: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

LocksLocksA lock knows what data it protects.Guarantees data can only be accessed when the lockis held.State is never accidentally shared."Lock data, not code" is enforced in Rust.

47 / 79

Page 67: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Thread-safety isn't justThread-safety isn't justdocumentation; it's law.documentation; it's law.

Every data type knows whether it can safely be sentbetween or accessed by multiple threads.Rust enforces this safe usage; there are no dataraces, even for lock-free data structures.

48 / 79

Page 68: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Message PassingMessage PassingA ! B

49 / 79

Page 69: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ChannelsChannels

std::sync::mpsc

fn channel<T>() -> (Sender<T>, Receiver<T>)fn send(&self, t: T) -> Result<(), SendError<T>>fn recv(&self) -> Result<T, RecvError> impl<T: Send> Send for Sender<T> { }

50 / 79

Page 70: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

This won't workThis won't worklet mut vec = Vec::new(); sender.send(vec); print_vec(&vec);

51 / 79

Page 71: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

error[E0382]: borrow of moved value: `vec` --> src/main.rs:13:15 |8 | let mut vec = Vec::new(); | ------- move occurs because `vec` has type `std::vec...11 | tx.send(vec); | --- value moved here12 |13 | print_vec(&vec); | ^^^^ value borrowed here after move

52 / 79

Page 72: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recvlet (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 73: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Create a channel and deconstuct the sender and receiver

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 74: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Spawn a new thread

std::thread::spawn(move || {

});

let (sender, receiver) = std::sync::mpsc::channel(); 

let result = expensive_computation(); sender.send(result).unwrap();

 match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 75: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Run some computation

let result = expensive_computation();

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || {

sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 76: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Send the result to the receiver

sender.send(result).unwrap();

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation();

}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 77: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Receive the result

match receiver.recv() {

}

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); 

Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)

53 / 79

Page 78: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Handle the success case

Ok(msg) => println!("{:?}", msg),

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() {

Err(err) => println!("{:?}", err)}

53 / 79

Page 79: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recv

Handle the error case

Err(err) => println!("{:?}", err)

let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg),

}

53 / 79

Page 80: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

spawn, send, recvspawn, send, recvlet (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || { let result = expensive_computation(); sender.send(result).unwrap();}); match receiver.recv() { Ok(msg) => println!("{:?}", msg), Err(err) => println!("{:?}", err)}

53 / 79

Page 81: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Equivalent in ElixirEquivalent in Elixirparent = self() spawn(fn -> result = expensive_computation() send(parent, result)end) receive do msg -> IO.inspect(msg)end

54 / 79

Page 82: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

For concurrency, this means you can choose from a widevariety of paradigms (message passing, shared state, lock-

free, purely functional), and Rust will help you avoidcommon pitfalls.

55 / 79

Page 83: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

BEAM + RustBEAM + Rust

56 / 79

Page 84: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

PortsNIFsNodes

57 / 79

Page 85: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

PortsPortsCommunicate with external programs over stdin/stdout

58 / 79

Page 86: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a portport = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)

59 / 79

Page 87: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a port

Open a port to an external binary

port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)

59 / 79

Page 88: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a port

Serialize our data to binary

data = :erlang.term_to_binary(term)

port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) 

len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)

59 / 79

Page 89: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a port

Get the length of the encoded data

len = byte_size(data)

port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)

iodata = [<<len::big-unsigned-integer-size(64)>>, data] Port.command(port, iodata)

59 / 79

Page 90: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a port

Pack our length and data into an IO list

iodata = [<<len::big-unsigned-integer-size(64)>>, data]

port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)

 Port.command(port, iodata)

59 / 79

Page 91: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Sending data to a portSending data to a port

Send it to the port

Port.command(port, iodata)

port = Port.open({:spawn_executable, "priv/binary"}, [:binary]) data = :erlang.term_to_binary(term)len = byte_size(data)iodata = [<<len::big-unsigned-integer-size(64)>>, data] 

59 / 79

Page 92: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Erlang Terms in RustErlang Terms in Rust

A Rust implementation of Erlang External Term Format

https://crates.io/crates/eetf

60 / 79

Page 93: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

pub enum Term { Atom(Atom), FixInteger(FixInteger), BigInteger(BigInteger), Float(Float), Pid(Pid), Port(Port), Reference(Reference), Binary(Binary), BitBinary(BitBinary), List(List), ImproperList(ImproperList), Tuple(Tuple), Map(Map),}

61 / 79

Page 94: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

use eetf::Term; let (tx, rx) = std::sync::mpsc::channel::<Term>(); std::thread::spawn(move || { let stdin = io::stdin(); let mut locked = stdin.lock(); let mut term_buffer = Vec::new();  loop { // ... }});

62 / 79

Page 95: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 96: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Start an infinite loop

loop {

}

let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();

63 / 79

Page 97: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Create an 8-byte buffer

let mut buffer = [0; 8];loop {

if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 98: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Read 8-bytes from stdin

if let Err(_) = locked.read_exact(&mut buffer) { break; };

loop { let mut buffer = [0; 8];

  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 99: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Find the length of the data

let length = u64::from_be_bytes(buffer);

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; }; 

term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 100: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Resize the term buffer to accomodate the size of the data

term_buffer.resize(length as usize, 0);

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer);

if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 101: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Read the data into the term buffer

if let Err(_) = locked.read_exact(&mut term_buffer) { break; };

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0);

  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 102: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Decode the binary data into a Term

let term = Term::decode(Cursor::new(&term_buffer)).unwrap();

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; }; 

tx.send(term).unwrap();}

63 / 79

Page 103: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Send the Term to the reciever thread

tx.send(term).unwrap();

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap();

}

63 / 79

Page 104: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

loop { let mut buffer = [0; 8]; if let Err(_) = locked.read_exact(&mut buffer) { break; };  let length = u64::from_be_bytes(buffer); term_buffer.resize(length as usize, 0); if let Err(_) = locked.read_exact(&mut term_buffer) { break; };  let term = Term::decode(Cursor::new(&term_buffer)).unwrap(); tx.send(term).unwrap();}

63 / 79

Page 105: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

while let Ok(term) = rx.recv() { match term { Term::FixInteger(FixInteger { value }) => // ..., Term::Atom(Atom { name }) => match name.as_ref() { "quit" => std::process::exit(0), text => println!("{}", text), }, other => println!("Received {:?}", other), }}

64 / 79

Page 106: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIFsNIFsNative Implemented Functions

65 / 79

Page 107: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

RustlerRustler

https://github.com/rusterlium/rustler

66 / 79

Page 108: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ExampleExampleImage processingImage processing

https://github.com/scrogson/mirage

67 / 79

Page 109: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

MirageMiragedefmodule Mirage do use Rustler, otp_app: :mirage  defstruct bytes: nil, byte_size: nil, extension: nil, height: nil, width: nil, resource: nil  def from_bytes(_path), do: :erlang.nif_error(:nif_not_loaded) def resize(_resource, _width, _height), do: :erlang.nif_error(:end

68 / 79

Page 110: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplateuse rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {

mirage::load(env)

69 / 79

Page 111: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

Setup the mapping from BEAM MFAs to NIF functions

rustler::rustler_export_nifs! {

}

 mod atoms;mod mirage; 

"Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)

 fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}

69 / 79

Page 112: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

Module name

"Elixir.Mirage",

use rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! {

[ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {

mirage::load(env)

69 / 79

Page 113: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

("from_bytes", 1, mirage::from_bytes, DirtyIo),

use rustler::schedule::SchedulerFlags::*; mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [

("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}

69 / 79

Page 114: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

("resize", 3, mirage::resize, DirtyCpu),

 mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo),

], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}

69 / 79

Page 115: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

Function to call when the NIF loads

Some(load)

 mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ],

} fn load(env: rustler::Env, _info: rustler::Term) -> bool { mirage::load(env)}

69 / 79

Page 116: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

fn load(env: rustler::Env, _info: rustler::Term) -> bool {

}

 mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} 

mirage::load(env)

69 / 79

Page 117: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NIF BoilerplateNIF Boilerplate

Setup resource objects

mirage::load(env)

 mod atoms;mod mirage; rustler::rustler_export_nifs! { "Elixir.Mirage", [ ("from_bytes", 1, mirage::from_bytes, DirtyIo), ("resize", 3, mirage::resize, DirtyCpu), ], Some(load)} fn load(env: rustler::Env, _info: rustler::Term) -> bool {

}

69 / 79

Page 118: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Resource ObjectsResource Objects

A way to pass pointers back and forth

struct Image { image: DynamicImage, format: ImageFormat,} pub fn load(env: Env) -> bool { rustler::resource_struct_init!(Image, env); true}

70 / 79

Page 119: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Elixir StructElixir StructEncoding/DecodingEncoding/Decoding

%Mirage{}

defstruct byte_size: nil, extension: nil, height: nil, width: nil, resource: nil

71 / 79

Page 120: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Elixir StructElixir StructEncoding/DecodingEncoding/Decoding

#[derive(NifStruct)]#[module = "Mirage"]struct Mirage { byte_size: usize, extension: Atom, height: u32, width: u32, resource: ResourceArc<Image>,}

72 / 79

Page 121: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { let bytes: Binary = args[0].decode()?;  match image::load_from_memory(bytes.as_slice()) { // ... }}

73 / 79

Page 122: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

<'a> = lifetime annotations

pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {

}

let bytes: Binary = args[0].decode()?;  match image::load_from_memory(bytes.as_slice()) { // ... }

73 / 79

Page 123: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Decode the first argument into a Binary

let bytes: Binary = args[0].decode()?;

pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {

  match image::load_from_memory(bytes.as_slice()) { // ... }}

73 / 79

Page 124: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Load the bytes into an image::Image

match image::load_from_memory(bytes.as_slice()) {

}

pub fn from_bytes<'a>( env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> { let bytes: Binary = args[0].decode()?; 

// ...

}

73 / 79

Page 125: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };  return Ok((ok(), mirage).encode(env));}return Err(rustler::Error::Atom("unsupported_image_format"));

74 / 79

Page 126: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

If we have a valid image format

if let Ok(format) = image::guess_format(&bytes.as_slice()) {

}

let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };  return Ok((ok(), mirage).encode(env));

return Err(rustler::Error::Atom("unsupported_image_format"));

74 / 79

Page 127: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Build up our Mirage struct

let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };

if let Ok(format) = image::guess_format(&bytes.as_slice()) {

  return Ok((ok(), mirage).encode(env));}return Err(rustler::Error::Atom("unsupported_image_format"));

74 / 79

Page 128: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Return {:ok, %Mirage{}}

return Ok((ok(), mirage).encode(env));

if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), }; 

}return Err(rustler::Error::Atom("unsupported_image_format"));

74 / 79

Page 129: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

Return {:error, :unsupported_image_format}

return Err(rustler::Error::Atom("unsupported_image_format"));

if let Ok(format) = image::guess_format(&bytes.as_slice()) { let mirage = Mirage { byte_size: bytes.len(), extension: extension(format), width: image.width(), height: image.height(), resource: ResourceArc::new(Image { image, format }), };  return Ok((ok(), mirage).encode(env));}

74 / 79

Page 130: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

NodesNodesCommunicate with applications using the Erlang

Distribution Protocol

75 / 79

Page 131: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

erl_disterl_dist

Rust Implementation of Erlang Distribution Protocol

https://crates.io/crates/erl_dist

76 / 79

Page 132: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

eiei

Rust Implementation of erl_interface

https://crates.io/crates/ei

77 / 79

Page 133: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

ConclusionConclusion

78 / 79

Page 134: BEAM + Rust...Reliability Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — and enable you to eliminate many classes of bugs at compile-time.

79 / 79