Share
What is Rust?
You may have seen Rust on Hacker News, Reddit or Twitter, but what is all the hype about? With Rust just recently having become stable, now’s a great time to learn more about this emerging language that is taking on old stalwarts like C++ without giving up on the expressiveness of higher level languages.
In this talk, Yehuda will talk about Rust’s ownership system, which provides automatic memory management without paying the runtime cost of a garbage collector or reference counting. Instead, Rust guarantees, at compile time, that your program will never segfault. Learn how that can work, and how the same approach makes your Rust code remarkably resilient against memory leaks.
Whether you’re a grizzled systems programmer or a dynamic language programmer looking to dip your toe in, this talk will have something for you.
Yehuda Katz
Transcript
170 slides
-
What is Systems Programming? • Programming without a GC •
Programming directly against the metal, without abstraction costs • Pay-as-you-go, lightweight runtime, or no runtime • Zero-cost abstractions • When "it doesn't end up mattering" isn't true -
That Doesn't Need to Mean • malloc and free •
Assembly Language • Targeting Unix-only or Windows-only • Old-school tools like hand-crafted Makefiles • Fighting with the linker • Writing your entire application in a systems language -
Garbage Collection class Point def initialize(x, y)
@x, @y = x, y end end class Line def initialize(p1, p2) @p1, @p2 = p1, p2 end def length # compute length end end def length(x1, y1, x2, y2) p1 = Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) Point.new(x2, y2) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) Point.new(x2, y2) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) Point.new(x2, y2) Line.new(p1, p2) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) Point.new(x2, y2) Line.new(p1, p2) -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end Point.new(x1, y1) Point.new(x2, y2) Line.new(p1, p2) later… stop the world… -
Garbage Collection def length(x1, y1, x2, y2) p1 =
Point.new(x1, y1) p2 = Point.new(x2, y2) line = Line.new(p1, p2) line.length end -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } -
Right to Destroy pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); println!("Title: {}", book.title); } note the lack of manual memory management -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } -
Transferring Ownership pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: Book) { println!("Title: {}", book.title); } error: use of moved value: `book.chapters` note: `book` moved here -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Borrowing pub struct Book { title: String,
chapters: Vec<String> } fn main() { let book = read("book1.txt"); print_book(&book); println!("Chapters: {}", book.chapters.len()); } fn print_book(book: &Book) { println!("Title: {}", book.title); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Sub-Leasing fn main() { let book = read_book("book1.txt");
print_book(&book); } pub struct Book { title: String, chapters: Vec<String> } fn print_book(book: &Book) { print_title(&book); print_chapters(&book); } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } -
Return this by 5pm on Friday and feel free to
change it ✏️ -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
fn main() { let mut book = read_book("book1.txt");
print_book(&mut book); } pub struct Book { title: String, chapters: Vec<String>, bookmark: u32 } fn print_book(book: &mut Book) { print_title(&book); print_chapters(&book); book.bookmark = 100; } fn print_title(book: &Book) { println!("Title: {}", book.title); } fn print_chapters(book: &Book) { let len = book.chapters.len(); println!("Chapters: {}", len); } Sub-Leasing -
Rules of Borrowing • You can have many outstanding read-only
borrows at once • A mutable borrow is unique: no other borrows, mutable or read-only, can occur at the same time -
Rules of Borrowing • You can have many outstanding read-only
borrows at once • A mutable borrow is unique: no other borrows, mutable or read-only, can occur at the same time • These rules are enforced by the compiler, not at runtime -
Read-Only Borrows fn main() { let book1 =
read_book("book1.txt"); let book2 = read_book("book2.txt"); same(&book1, &book2); } pub struct Book { title: String, chapters: Vec<String> } fn same(a: &Book, b: &Book) -‐> bool { a.title == b.title && a.chapters == b.chapters } -
Read-Only Borrows fn main() { let book1 =
read_book("book1.txt"); let book2 = read_book("book2.txt"); same(&book1, &book2); } pub struct Book { title: String, chapters: Vec<String> } fn same(a: &Book, b: &Book) -‐> bool { a.title == b.title && a.chapters == b.chapters } ✅ -
Two Read-Only Borrows fn main() { let book1
= read_book("book1.txt"); same(&book1, &book1); } pub struct Book { title: String, chapters: Vec<String> } fn same(a: &Book, b: &Book) -‐> bool { a.title == b.title && a.chapters == b.chapters } -
Two Read-Only Borrows fn main() { let book1
= read_book("book1.txt"); same(&book1, &book1); } pub struct Book { title: String, chapters: Vec<String> } fn same(a: &Book, b: &Book) -‐> bool { a.title == b.title && a.chapters == b.chapters } ✅ -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let book1 = read_book("book1.txt"); let mut book2 = read_book("book2.txt"); copy(&book1, &mut book2); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let book1 = read_book("book1.txt"); let mut book2 = read_book("book2.txt"); copy(&book1, &mut book2); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } ✅ -
pub struct Book { title: String,
chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } Mutable Borrows Must Be Unique -
pub struct Book { title: String,
chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } Mutable Borrows Must Be Unique -
pub struct Book { title: String,
chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } Mutable Borrows Must Be Unique ^~~~ error: cannot borrow `book` as mutable because it is also borrowed as immutable -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } ^~~~ note: previous borrow of `book` occurs here -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } -
Mutable Borrows Must Be Unique pub struct Book {
title: String, chapters: Vec<String> } fn main() { let mut book = read_book("book1.txt"); copy(&book, &mut book); } fn copy(a: &Book, b: &mut Book) { b.title = a.title; } ^ note: previous borrow ends here -
Closures fn main() { println!("{:?}", threes_squared(10)); }
fn threes_squared(upto: u32) -‐> Vec<u32> { (0..upto) .filter(|i| i % 3 == 0) .map(|i| i.pow(2)) .collect() } -
Closed Over Variables fn main() { println!("{:?}", threes_squared(10,
"LOG")); } fn threes_squared(upto: u32, string: &str) -‐> Vec<String> { (0..upto) .filter(|i| i % 3 == 0) .map(|i| format!("{}: {}", string, i.pow(2))) .collect() } -
Equivalent Performance fn main() { println!("{:?}", threes_squared(10, "LOG"));
} fn threes_squared(upto: u32, string: &str) -‐> Vec<String> { let v = vec![]; loop { if i % 3 == 0 { v.push(format!("{}: {}", string, i * i)); } if i < upto { break; } } v } -
Details Abstracted fn main() { println!("{:?}", threes_squared(10, "LOG"));
} fn threes_squared(upto: u32, string: &str) -‐> Vec<String> { let v = Vec::with_capacity(upto -‐ 1); loop { if i % 3 == 0 { v.push(format!("{}: {}", string, i * i)); } if i < upto { break; } } v } -
Details Abstracted fn main() { println!("{:?}", threes_squared(10, "LOG"));
} fn threes_squared(upto: u32, string: &str) -‐> Vec<String> { let v = Vec::with_capacity(upto -‐ 1); loop { if i % 3 == 0 { v.push(format!("{}: {}", string, i * i)); } if i < upto { break; } } v } Bounds Checks -
FnOnce A closure that only runs once and can take
ownership from its environment Fn A closure that cannot mutate its environment and can be shared across threads FnMut A closure that can mutate its environment but cannot be shared across threads -
FnOnce fn main() { let buf = String::new();
stdin().read_line(&mut buf).map(|size| { println!("Read {} bytes", size); transfer(buf); }); } fn transfer(string: String) { println!("Got transferred {:?}", string); } -
Rules of Borrowing • You can have many outstanding read-only
borrows at once • A mutable borrow is unique: no other borrows, mutable or read-only, can occur at the same time -
Rules of Borrowing • You can have many outstanding read-only
borrows at once • A mutable borrow is unique: no other borrows, mutable or read-only, can occur at the same time • You can alias or mutate, but not both at the same time -
Send Safe to transfer across threads (no borrowed values) Sync
Safe to share across threads (no racy "interior mutability") -
spawn() fn main() { let book = read_book("book1.txt");
spawn(|| { println!("{}", book); }); } -
spawn() fn main() { let book = read_book("book1.txt");
spawn(|| { println!("{}", book); }); } -
spawn() fn main() { let book = read_book("book1.txt");
spawn(|| { println!("{}", book); }); } ^~~~ error: `book` does not live long enough -
spawn() fn main() { let book = read_book("book1.txt");
spawn(move || { println!("{}", book); }); } -
scoped() fn main() { let book = read_book("book1.txt");
let guard = scoped(|| { println!("{}", book); }); // do other work let output = guard.join(); } -
scoped() fn main() { let book = read_book("book1.txt");
let guard = scoped(|| { println!("{}", book); }); // do other work let output = guard.join(); // default impl of Drop } -
OO Syntax struct Circle { x: f64,
y: f64, radius: f64, } impl Circle { fn area(&self) -‐> f64 { std::f64::consts::PI * (self.radius * self.radius) } }