tour of rust
TRANSCRIPT
![Page 1: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/1.jpg)
Tour of Rust
Seo Sanghyeon2016-08-18
![Page 2: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/2.jpg)
Control and Safety• C++
More control, less safety• Java
Less control, more safety• Rust
More control, more safety
![Page 3: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/5.jpg)
Boxed representation
xy
widthheight
originsize
rect
![Page 6: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/6.jpg)
Unboxed representation
xy
widthheight
![Page 7: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/7.jpg)
Assignment• b = a, by default,
Reference in PythonCopy in C++Move in Rust
![Page 8: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/8.jpg)
Reference (Python)a = [1, 2, 3]b = aa[2] = 4b[2] # evaluates to 4
![Page 9: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/9.jpg)
Reference
1 2 3a
b
4
![Page 10: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/11.jpg)
Copy
1 2 3a
b 1 2 3
4
![Page 12: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/14.jpg)
Move
1 2 3a
b
![Page 15: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/16.jpg)
Move (C++)std::vector<int> a, b;a = {1, 2, 3};b = std::move(a);a[2] = 4; // crashes
![Page 17: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/17.jpg)
Move (Rust), 1let a = vec![1, 2, 3];let b = a;a[2] = 4;
![Page 18: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/19.jpg)
Move (Rust), 2let mut a = vec![1, 2, 3];let b = a;a[2] = 4;
![Page 20: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/20.jpg)
Move is enforced| let b = a;| - value moved here| a[2] = 4;| ^ value used here after move
![Page 21: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/21.jpg)
Conditional movelet a = vec![1, 2, 3];if random() { let b = a;}// is a destroyed?
![Page 22: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/23.jpg)
Move based on type• In Rust, b = a
Move by defaultCopy if a: T and T: Copy
![Page 24: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/24.jpg)
Copy is opt-instruct Point { x: i32, y: i32,}
impl Copy for Point {}
![Page 25: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/26.jpg)
Copy for references• &T: Copy
Immutable reference is copied• &mut T: !Copy
Mutable reference cannot be copied
![Page 27: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/27.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/28.jpg)
Invalidation (Rust)let mut a = vec![];a.push(1);let b = &a[0];a.push(2);
![Page 29: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/29.jpg)
Compile time check| let b = &a[0];| - immutable borrow| a.push(2);| ^ mutable borrow
![Page 30: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/30.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/31.jpg)
Aliasing XOR Mutation• Multiple immutable borrows• Or single mutable borrow
• This is why &mut T cannot be copied
![Page 32: Tour of Rust](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/32.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/33.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/34.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/35.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/36.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081604/5880d1cd1a28ab9c3a8b563f/html5/thumbnails/37.jpg)
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