Rust has something unique to offer that languages in that space have never had before, and that is a degree of safety that languages like C and C++ have never had. Rust promises to deliver equivalent or better performance and greater productivity with guaranteed memory safety and data race freedom while allowing complete and direct control over memory.
This video will cover:
What is Rust?
Benefits of Rust
Rust Ecosystem
Popular Applications in Rust
2. Rust
What is Rust ?
Rust is a systems programming language with a focus on
safety, especially safe concurrency, supporting both
functional and imperative paradigms. Rust is syntactically
similar to C++, but its designers intend it to provide better
memory safety while still maintaining performance.
Rust was originally designed by Graydon Hoare at
Mozilla Research, with contributions from Dave Herman,
Brendan Eich, and many others. Rust won first place for
"most loved programming language" in the Stack Overflow
Developer Survey in 2016, 2017, and 2018
3. Why Rust ?
Empowering Everyone to build efficient and reliable software
● Performance
○ Rust is blazingly fast and memory-efficient: with no runtime or garbage collector, it
can power performance-critical services, run on embedded devices, and easily
integrate with other languages.
● Reliability
○ Rust’s rich type system and ownership model guarantee memory-safety and thread-
safety — and enable you to eliminate many classes of bugs at compile-time.
● Productivity
○ Rust has great documentation, a friendly compiler with useful error messages, and
top-notch tooling — an integrated package manager and build tool, smart multi-
editor support with auto-completion and type inspections, an auto-formatter, and
more.
5. What is Safe
?
Lorem Ipsum comes from section Contrary to
popular belief, Lorem Ipsum is not simply random
text.
● Mutability
● Multiple references to same variable.
6. Memory Safety ?
● Use after Free (dangling pointers)
● Double Free.
● Null Pointer Dereference
8. Memory Safety ?
Double Free
A double free in C, technically speaking, leads to undefined behavior. This means that the
program can behave completely arbitrarily and all bets are off about what happens.
9. Memory Safety ?
Null Pointer Dereference
A NULL pointer points to memory that doesn't exist. This may be address 0x00000000 or any
other implementation-defined value (as long as it can never be a real address). Dereferencing it
means trying to access whatever is pointed to by the pointer.
13. Memory Safety?
Garbage Collector
● 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.
14. Memory Safety?
Garbage Collector (Some Thoughts)
● Computer cannot know the exact timing that each object should be freed –
tracing.
● GC:
○ tracing : 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.
● No predictability – cannot used for real-time system
● Limited concurrency – global interpreter lock
15. Memory Safety?
Garbage Collector (Some Thoughts)
● No predictability
○ cannot used for real-time system
● Limited concurrency
○ global interpreter lock
● Larger code size
○ VM(or GC) must included
16. Memory Safety?
System Program
● Must be fast enough.
● Must have little runtime overhead.
● Must be Memory Safe
● Should be possible to have a direct memory access with safety
Garbage Collector can not provide it because of the overhead involved.
17. The Rust Way?
Compile time checks
Rust tries to achieve Safety and Control by pushing as many checks to the
compile time.
This is achieved mainly through the following concepts :
● Ownership.
● Borrowing
● Lifetimes
23. Borrowing
Borrowing is about how can we have multiple references to the same object
● Achieved with &T operator
● Rust has both Mutable and Immutable variable (Immutable by default)
24. Borrowing
error: cannot borrow `v` as mutable because it is
also borrowed as immutable
v.push(34);
^
note: previous borrow of `v` occurs here; the
immutable borrow prevents
subsequent moves or mutable borrows of `v` until
the borrow ends
25. Borrowing
Rules
○ First, any borrow must last for a scope no greater than that of the owner.
○ Second, you may have one or the other of these two kinds of borrows,
but not both at the same time:
■ one or more references (&T) to a resource,
■ exactly one mutable reference (&mut T).
28. Lifetime
Lending out a reference to a resource that someone else owns can be
complicated. For example, imagine this set of operations:
1. I acquire a handle to some kind of resource.
2. I lend you a reference to the resource.
3. I decide I’m done with the resource, and deallocate it, while you still have
your reference.
4. You decide to use the resource.
29. Lifetime
The ownership system in Rust does this through a concept called lifetimes,
which describe the scope that a reference is valid for.
● Owner will always have power to deallocate or destroy a source.
● In Simple cases compiler is capable enough to identify the problem and give
you an error.
● In complex cases there is a way where you can provide hint to compiler
using ‘a keyword.
30. Lifetime
The ownership system in Rust does this through a concept called lifetimes,
which describe the scope that a reference is valid for.
● Owner will always have power to deallocate or destroy a source.
● In Simple cases compiler is capable enough to identify the problem and give
you an error.
● In complex cases there is a way where you can provide hint to compiler
using ‘a keyword.
32. Cargo
● Cargo is the Rust package manager. Cargo downloads your Rust package’s
dependencies, compiles your packages, makes distributable packages, and
uploads them to crates.io, the Rust community’s package registry.
34. Rustup
● Rustup is the rust tool chain installer.
● Easily switch between stable, beta and nightly
● Cross compiling is much simpler
● Easy to install and work like Node’s NVM or python pyEnv
35. Rustfmt
● Rustfmt automatically formats Rust code
● making it easier to read, write, and maintain.
● never debate spacing or brace position ever again.
36. Clippy
● Clippy helps developers of all experience levels write idiomatic code, and
enforce standards.
37. Cargo Doc
● Cargo’s doc builder makes it so no API ever goes undocumented. It’s
available locally through cargo doc, and online for public crates through
docs.rs.