Learn More
Rust Primer
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
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.
Rust
Safety and Control
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.
Memory Safety ?
● Use after Free (dangling pointers)
● Double Free.
● Null Pointer Dereference
Memory Safety ?
Use After Free
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.
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.
Memory Safety ?
Memory Safety ?
Mere pass Garbage Collector
hai ?
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.
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
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
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.
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
Ownership?
Ownership
Ownership
Ownership
Ownership
Ownership
Vijay does not have Maa
now she is with Ravi now.
Ownership
Ownership
Ownership
Ownership
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)
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
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).
Borrowing
Rules
Borrowing
Rules
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.
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.
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.
Concurrency in
Rust
● Threads
● Message Passing
● Shared State using Mutex
● Extensible Concurrency - Send and Sync Traits
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.
Cargo
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
Rustfmt
● Rustfmt automatically formats Rust code
● making it easier to read, write, and maintain.
● never debate spacing or brace position ever again.
Clippy
● Clippy helps developers of all experience levels write idiomatic code, and
enforce standards.
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.
IDE/Editors
Thank You!

Rust Primer

  • 1.
  • 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 ? EmpoweringEveryone 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.
  • 4.
  • 5.
    What is Safe ? LoremIpsum 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
  • 7.
  • 8.
    Memory Safety ? DoubleFree 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 ? NullPointer 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.
  • 10.
  • 11.
  • 12.
    Mere pass GarbageCollector hai ?
  • 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? Compiletime 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
  • 18.
  • 19.
  • 20.
    Ownership Ownership Vijay does nothave Maa now she is with Ravi now.
  • 21.
  • 22.
  • 23.
    Borrowing Borrowing is abouthow 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, anyborrow 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).
  • 26.
  • 27.
  • 28.
    Lifetime Lending out areference 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 systemin 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 systemin 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.
  • 31.
    Concurrency in Rust ● Threads ●Message Passing ● Shared State using Mutex ● Extensible Concurrency - Send and Sync Traits
  • 32.
    Cargo ● Cargo isthe 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.
  • 33.
  • 34.
    Rustup ● Rustup isthe 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 automaticallyformats Rust code ● making it easier to read, write, and maintain. ● never debate spacing or brace position ever again.
  • 36.
    Clippy ● Clippy helpsdevelopers of all experience levels write idiomatic code, and enforce standards.
  • 37.
    Cargo Doc ● Cargo’sdoc builder makes it so no API ever goes undocumented. It’s available locally through cargo doc, and online for public crates through docs.rs.
  • 38.
  • 39.