Rust means Discipline
Talk on Rust's Reliability, Ownership and Borrow-checker
MYSELF
Sr Data Scientist @
Contributed 61 PRs to Rust ecosystem
Highest starred repository: 2022-rustlings-solutions
Short Stories Writer
Sr Data Scientist @
Python, Rust, Zig
Machine Learning, Data Engineer
1
In Android 13, Google announced that approximately 21% of the new native code is Rust.
3
Mark Russinovich, the CTO of Microsoft Azure, stated that C and C++ should no longer be used for new projects. He suggested Rust should be used instead.
5
Stylo, the CSS engine in Firefox, developers were able to replace about 160,000 lines of C++ code with only 85,000 lines of Rust code.
4
Discord, switched from Go to Rust to avoid the latency spikes they used to experience with the Read States service
Source: https://yalantis.com/blog/rust-market-overview/, https://rustmagazine.org/issue-1/2022-review-the-adoption-of-rust-in-business/
Rust is Reliable, C is Not
C's Addition
Rust's Addition
Let's run the code for C & Rust
Let's run the code for C & Rust
C's assembly
Rust's assembly
C division
Rust division
Let's run the code for C & Rust
Let's run the code for C & Rust
C's assembly (From gcc)
Rust's assembly (From rustc)
Check for zero in denominator
Check for overflow: 128
Check for zero in denominator
Relevant errors are hardcoded into the binary
Rust's Ownership
- Each value in Rust has an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
Let's understand why Ownership is required
Ref: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html
Ref: https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#references-and-borrowing
Let's use references
What is it ?
- References are pointers.
- They never point to invalid memory.
- They never own value.
Mutable references
- Mutable references are simply references that can mutate the data.
Borrow-Checker
- Cannot have more than one mutable reference in a scope, if value is accessed.
- Cannot have immutable reference followed by mutable reference, if immutable reference is accessed.
- No dangling references, i.e null pointers.
What is it ?
Two Mutable borrows
Error for borrowing "s" as mutable twice
This is to avoid any
data race conditions
https://www.reddit.com/r/rust/comments/v3vagi/data_races_explanation_in_the_rust_book/
Immutable and mutable borrows
Error for using immutable borrow while mutable borrow is alive
This is to avoid change in value while immutable borrow is still alive
Dangling Pointer
Errors because "s" goes out of scope and there's nothing to reference
This is to avoid any null pointer
Thank You!
Questions?
Swecha- servicenow
By Akhil G
Swecha- servicenow
- 157