Tour of Rust

37
Tour of Rust Seo Sanghyeon 2016-08-18

Transcript of Tour of Rust

Page 1: Tour of Rust

Tour of Rust

Seo Sanghyeon2016-08-18

Page 2: Tour of Rust

Control and Safety• C++

More control, less safety• Java

Less control, more safety• Rust

More control, more safety

Page 3: Tour of Rust

Users• Rust

Rust compiler is written in Rust• Servo

Parallel browser engine• Dropbox

Cloud file storage• More on

https://www.rust-lang.org/en-US/friends.html

Page 4: Tour of Rust

Memory representation

struct Point { x: i32, y: i32 }

struct Size { width: i32, height: i32 }

struct Rect { origin: Point, size: Size }

Page 5: Tour of Rust

Boxed representation

xy

widthheight

originsize

rect

Page 6: Tour of Rust

Unboxed representation

xy

widthheight

Page 7: Tour of Rust

Assignment• b = a, by default,

Reference in PythonCopy in C++Move in Rust

Page 8: Tour of Rust

Reference (Python)a = [1, 2, 3]b = aa[2] = 4b[2] # evaluates to 4

Page 9: Tour of Rust

Reference

1 2 3a

b

4

Page 10: Tour of Rust

Copy (C++)std::vector<int> a, b;a = {1, 2, 3};b = a;a[2] = 4;b[2]; // evaluates to 3

Page 11: Tour of Rust

Copy

1 2 3a

b 1 2 3

4

Page 12: Tour of Rust

Copy (C++) and destructionstd::vector<int> a, b;a = {1, 2, 3};b = a;// b is destroyed// a is destroyed

Page 13: Tour of Rust

Move (Rust) and destructionlet a = vec![1, 2, 3];let b = a;// a is moved and can’t be used// any more// b is destroyed// a is not destroyed

Page 14: Tour of Rust

Move

1 2 3a

b

Page 15: Tour of Rust

Comparison

Python C++ Rust

Reference b = a T& b = a b = &a

Copy b = list(a) b = a b = a.clone()

Move b = move(a) b = a

Page 16: Tour of Rust

Move (C++)std::vector<int> a, b;a = {1, 2, 3};b = std::move(a);a[2] = 4; // crashes

Page 17: Tour of Rust

Move (Rust), 1let a = vec![1, 2, 3];let b = a;a[2] = 4;

Page 18: Tour of Rust

Immutable by default| let a = vec![1, 2, 3];| - use `mut a` here| let b = a;| a[2] = 4;| ^ cannot borrow mutably

Page 19: Tour of Rust

Move (Rust), 2let mut a = vec![1, 2, 3];let b = a;a[2] = 4;

Page 20: Tour of Rust

Move is enforced| let b = a;| - value moved here| a[2] = 4;| ^ value used here after move

Page 21: Tour of Rust

Conditional movelet a = vec![1, 2, 3];if random() { let b = a;}// is a destroyed?

Page 22: Tour of Rust

Move flaglet a = vec![1, 2, 3];let a_flag = true;if random() { let a_flag = false; let b = a;}if a_flag { /* a is destroyed */ }

Page 23: Tour of Rust

Move based on type• In Rust, b = a

Move by defaultCopy if a: T and T: Copy

Page 24: Tour of Rust

Copy is opt-instruct Point { x: i32, y: i32,}

impl Copy for Point {}

Page 25: Tour of Rust

Copy impl is checkedstruct Point { name: String, x: i32, y: i32,}

impl Copy for Point {}// error: field `name` does not implement Copy

Page 26: Tour of Rust

Copy for references• &T: Copy

Immutable reference is copied• &mut T: !Copy

Mutable reference cannot be copied

Page 27: Tour of Rust

Invalidation (C++)std::vector<int> a;a.push_back(1);int& b = a[0];a.push_back(2);b; // undefined

Page 28: Tour of Rust

Invalidation (Rust)let mut a = vec![];a.push(1);let b = &a[0];a.push(2);

Page 29: Tour of Rust

Compile time check| let b = &a[0];| - immutable borrow| a.push(2);| ^ mutable borrow

Page 30: Tour of Rust

Borrow is scopedlet mut a = vec![];a.push(1);{ let b = &a[0]; b; // this is okay!}a.push(2); // this too!

Page 31: Tour of Rust

Aliasing XOR Mutation• Multiple immutable borrows• Or single mutable borrow

• This is why &mut T cannot be copied

Page 32: Tour of Rust

Functionfn get(v: &Vec<i32>, i: usize) -> &i32 { &v[i] }

let mut a = vec![];a.push(1);let b = get(&a, 0);a.push(2);

Page 33: Tour of Rust

Lifetimefn get<’a>(v: &’a Vec<i32>, i: usize) -> &’a i32 { &v[i] }

let mut a = vec![];a.push(1);let b = get(&a, 0);a.push(2);

Page 34: Tour of Rust

Lifetime subtyping• If ’a: ’b (’a outlives ’b)

&’a T <: &’b T

• Converse is also true:If &’a T <: &’b T’a: ’b

Page 35: Tour of Rust

Lifetime inferencefn get<’a>(v: &’a Vec<i32>, i: usize) -> &’a i32 { &v[i] }

let b: &’x i32 = get(&’y a, 0);// what is relationship between// ’x and ’y?

Page 36: Tour of Rust

Inference steps• fn get <: get• &’a T -> &’a U <: &’y T -> &’x U• &’y T <: &’a T, &’a U <: &’x U• ’y: ’a, ’a: ’x• ’y: ’x

Page 37: Tour of Rust

Lifetime inference resultlet b: &’x i32 = get(&’y a, 0);// ’y outlives ’x// a is borrowed as long as// b is in scope// which was to be demonstrated