Download - Rust Programming Language
![Page 1: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/1.jpg)
Introduction to Rust
![Page 2: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/2.jpg)
Brief introduction
• First Inventor: Graydon Hoare (about 8 years)• Mozilla foundation(2009~)• Trait-based OOP• Zero-cost abstraction• High concurrency support
• Memory safety by ownership and lifetime concept
![Page 3: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/3.jpg)
Problem: Memory Safety
• Use-after-free (dangling pointer)
• Double free
• Null pointer dereference
These kind of problems cause not only software crash, but also security vulnerabilities.
![Page 4: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/4.jpg)
Use-after free
Simple and trivial case
![Page 5: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/5.jpg)
Dangling pointer
&
Local variable is allocated in stack, a temporal storage of function.
If you return a reference of local variable,the address will be invalidated.
If these two functions are far away from each other, this kind of bugs can be very hard to find.
![Page 6: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/6.jpg)
Hard case: Iterator invalidation
Can you see the problem?
![Page 7: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/7.jpg)
Even a famous library may betray you
If you do not know much about the internals...
![Page 8: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/8.jpg)
Garbage collection
• Java, Python, Ruby, C#, Scala, Go...• Programmer creates objects. However,
the computer is responsible to remove them.
• No explicit malloc and free. – Therefore no mistake.
Is the world saved?
![Page 9: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/9.jpg)
The real life is not that easy...
• Computer cannot know the exact timing that each object should be freed.– tracing GC:GC engine should track all objects
periodically.– reference counting: every object has a
counter; the number of pointers referencing itself.
• Both ways need more memory and CPU power.
![Page 10: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/10.jpg)
Garbage Collection
• No predictability– cannot used for real-time system
• Limited concurrency– global interpreter lock
• Larger code size– VM(or GC) must included
![Page 11: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/11.jpg)
System program
• Must be FAST.
• Must has runtime overhead as little as possible.
• Must be memory SAFE.
• Should be possible to direct memory access.
• GC cannot be used in such area!
![Page 12: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/12.jpg)
Rust programming language
• Zero-cost abstraction
• Memory safety without garbage collection
• Super fast code generation
• C function compatibility (extern "C")
• Simpler syntax than C++
![Page 13: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/13.jpg)
Basic: move sementic
http://is.gd/pZKiBw
![Page 14: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/14.jpg)
Basic: mutability
http://is.gd/OQDszP
![Page 15: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/15.jpg)
Benchmark
• http://benchmarksgame.alioth.debian.org/u64q/rust.html
• Program written in Rust is as fast as that of C!
![Page 16: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/16.jpg)
Reminder: iterator invalidation
![Page 17: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/17.jpg)
Case study: Servo
• Mozilla's next-gen web browser engine• Written in Rust• Parallel layout, rendering, ... almost
everything
• "During the 2 years of development, we have never experienced any memory-related bugs like use-after-free or double free."
- an engineer from Mozilla
![Page 18: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/18.jpg)
Performance enhencement
![Page 19: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/19.jpg)
http://is.gd/dEamuS
![Page 20: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/20.jpg)
The ownership magic
v is an owner of the vector
x borrows the vector from v
now v cannot modify the vectorbecause it lent the ownership to x
![Page 21: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/21.jpg)
remember this?
&
![Page 22: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/22.jpg)
http://is.gd/3MTsSC
![Page 23: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/23.jpg)
Lifetime
borrowed pointer cannot outlivesthe owner!!
![Page 24: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/24.jpg)
Borrowing rules
• You cannot borrow mutable reference from immutable object
• You can borrow immutable reference many times• You cannot borrow more than one mutable reference• There cannot exist a mutable reference and an
immutable one simultaneously
• The lifetime of a borrowed reference should be ended before the owner object do
![Page 25: Rust Programming Language](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a524221a28ab10768b4680/html5/thumbnails/25.jpg)