"Introduction to Rust" as the name describes it, is a session which briefs about what rust is. In the session, we talk about what Rust is, its impact on the programming universe, & it's features like memory safety and parallelism.
2. Agenda
● What is RUST?
● Fun Facts about RUST.
● What’s wrong with C/C++
● A Rusty solution
● Ownership
● Effects of ownership
3. What is RUST
● It is a systems programming language.
● Introduced as a potential replacement of C/C++.
● Research project at Mozilla since 2010.
● Released the first version in 2015.
● Its open source.
4. Fun Facts about Rust
● Most loved programming language in the survey on StackOverflow for
2016, 2017 and 2018.
● Has over 2200 GitHub contributors.
● Involved in multiple projects viz:
○ Firefox Servo browser engine and Firefox Quantum.
○ Microsoft Azure IoT edge.
○ Google Xi - OS Text editor.
5. What's wrong with C / C++
● Preferred languages for System Software.
● They compile code to binaries which machine understands.
● But they aren’t memory safe:
○ Ensure freeing up the memory acquired. (Memory Leak)
○ Ensure freed memory isn’t re-accessed.
○ Ensure memory isn’t freed up twice.
6. What's wrong with C / C++
● Require experience, patience and one may still make mistakes.
● Using garbage collector for memory management.
● Using GC has an effect of performance.
● GC doesn’t provide resource management.
● Interpreted language requires additional processing before binary code.
8. So where does Rust fit into this?
The outshining feature of Rust is:
• Just like GC intensive languages, you don’t explicitly free memory.
• Unlike GC intensive languages, you don’t explicitly release
resources.
• All this is achieved without any Runtime costs or sacrificing safety.
9. How Rust does it
• One of the main features of Rust is how it manages its memory.
• Memory management is governed by a set of rules which are checked at
compile time.
• All these rules report any violations at compile time.
• This feature is known as ownership.
• None of these rules slow down the application.
10. Rules of Ownership
1. Each value in Rust has a variable known as its Owner.
2. There can be only one owner at a time.
3. When the owner goes out of scope, the value will be dropped.
11. Scope of a variable
// The range within which a variable is valid.
{ //salutation variable not valid
let salutation = “hello”; // valid
//valid
} //invalid
● Scope is the range within which a variable is valid.
● salutation variable is valid till the time it encounters end of scope.
12. The Drop
● At the end of owner’s lifetime, the memory is freed.
● Code required to run before the freeing up of memory is put in a method
called drop( ).
● Compiler automatically calls it at the end of the scope of the owner.
● Can be customized for custom data types.
● Memory checks are statically verified.
13. The Move!
struct User {
name: String,
age: i32,
}
fn main( ) {
let user1 = User {
name: String::from(“Ayush”),
age: 25,
};
let user2 = user1;
}
14. Stack of main( )
Memory allocated on Heap
Name: Ayush
Age: 25
user1
15. Stack of main( )
Memory allocated on Heap
Name: Ayush
Age: 25
user1
user2
16. 1. Same memory location is owned by two variables.
2. We’ll end up freeing the same location twice at the end of the scope.
The Move!
Two major issues arise:
As a solution to the above, rust invalidates user1 which makes:
- single owner for the memory location.
- we only need to free space addressed by user2.
- user1 becomes inaccessible.
17. Stack of main( )
Memory allocated on Heap
Name: Ayush
Age: 25
user1
user2
18. Ownership with Functions
● Passing a variable as an argument transfers ownership to the called
function.
fn main ( ) {
let name: String = String::from(“ Ayush”);
do_something(name); // name is transferred out of scope to
// do_something & hence is inaccessible so forth
}
fn do_something (name: String){
//impl
}
19. ● We can use references to variable created in some other scope.
fn main ( ) {
let name: String = String::from(“ Ayush”);
do_something(&name); // name is borrowed from the scope to
// do_something & hence is still accessible
}
fn do_something (name: &String) {
//impl
}
Borrowing
20. References
➔ The Rust book doc.rust-lang.org/book
➔ Stack Overflow
➔ https://blog.skylight.io/rust-means-never-having-to-close-a-socke
Refer to Sourabh’s blog to get started on Rust
➔ https://blog.knoldus.com/rust-quick-start-exploring-cargo/