SlideShare uma empresa Scribd logo
1 de 39
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!

Mais conteúdo relacionado

Mais procurados

Rust-lang
Rust-langRust-lang
Kubernetes and container security
Kubernetes and container securityKubernetes and container security
Kubernetes and container security
Volodymyr Shynkar
 

Mais procurados (20)

Why rust?
Why rust?Why rust?
Why rust?
 
Rust-lang
Rust-langRust-lang
Rust-lang
 
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
 
Linux KVM環境におけるGPGPU活用最新動向
Linux KVM環境におけるGPGPU活用最新動向Linux KVM環境におけるGPGPU活用最新動向
Linux KVM環境におけるGPGPU活用最新動向
 
Rust programming-language
Rust programming-languageRust programming-language
Rust programming-language
 
Attacking thru HTTP Host header
Attacking thru HTTP Host headerAttacking thru HTTP Host header
Attacking thru HTTP Host header
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
 
Rust: Systems Programming for Everyone
Rust: Systems Programming for EveryoneRust: Systems Programming for Everyone
Rust: Systems Programming for Everyone
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an Overview
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
 
webservice scaling for newbie
webservice scaling for newbiewebservice scaling for newbie
webservice scaling for newbie
 
In the DOM, no one will hear you scream
In the DOM, no one will hear you screamIn the DOM, no one will hear you scream
In the DOM, no one will hear you scream
 
왜 쿠버네티스는 systemd로 cgroup을 관리하려고 할까요
왜 쿠버네티스는 systemd로 cgroup을 관리하려고 할까요왜 쿠버네티스는 systemd로 cgroup을 관리하려고 할까요
왜 쿠버네티스는 systemd로 cgroup을 관리하려고 할까요
 
Kubernetes and container security
Kubernetes and container securityKubernetes and container security
Kubernetes and container security
 
Docker Basics
Docker BasicsDocker Basics
Docker Basics
 
Kubernetes security
Kubernetes securityKubernetes security
Kubernetes security
 
Windows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCPWindows Registered I/O (RIO) vs IOCP
Windows Registered I/O (RIO) vs IOCP
 
A whirlwind tour of the LLVM optimizer
A whirlwind tour of the LLVM optimizerA whirlwind tour of the LLVM optimizer
A whirlwind tour of the LLVM optimizer
 
How to build massive service for advance
How to build massive service for advanceHow to build massive service for advance
How to build massive service for advance
 
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy PullingFaster Container Image Distribution on a Variety of Tools with Lazy Pulling
Faster Container Image Distribution on a Variety of Tools with Lazy Pulling
 

Semelhante a Rust Primer

Semelhante a Rust Primer (20)

The State of the Veil Framework
The State of the Veil FrameworkThe State of the Veil Framework
The State of the Veil Framework
 
AV Evasion with the Veil Framework
AV Evasion with the Veil FrameworkAV Evasion with the Veil Framework
AV Evasion with the Veil Framework
 
Sistemas Distribuidos
Sistemas DistribuidosSistemas Distribuidos
Sistemas Distribuidos
 
Clojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in LispClojure: Programming self-optimizing webapps in Lisp
Clojure: Programming self-optimizing webapps in Lisp
 
Distributed fun with etcd
Distributed fun with etcdDistributed fun with etcd
Distributed fun with etcd
 
Secure Developer Access at Decisiv
Secure Developer Access at DecisivSecure Developer Access at Decisiv
Secure Developer Access at Decisiv
 
The Rust Programming Language
The Rust Programming LanguageThe Rust Programming Language
The Rust Programming Language
 
Introduction to Rust (Presentation).pptx
Introduction to Rust (Presentation).pptxIntroduction to Rust (Presentation).pptx
Introduction to Rust (Presentation).pptx
 
Andrea Di Persio
Andrea Di PersioAndrea Di Persio
Andrea Di Persio
 
Managing Software Dependencies and the Supply Chain_ MIT EM.S20.pdf
Managing Software Dependencies and the Supply Chain_ MIT EM.S20.pdfManaging Software Dependencies and the Supply Chain_ MIT EM.S20.pdf
Managing Software Dependencies and the Supply Chain_ MIT EM.S20.pdf
 
Refactoring Applications for the XK7 and Future Hybrid Architectures
Refactoring Applications for the XK7 and Future Hybrid ArchitecturesRefactoring Applications for the XK7 and Future Hybrid Architectures
Refactoring Applications for the XK7 and Future Hybrid Architectures
 
Why_safe_programming_matters_and_why_Rust_.pdf
Why_safe_programming_matters_and_why_Rust_.pdfWhy_safe_programming_matters_and_why_Rust_.pdf
Why_safe_programming_matters_and_why_Rust_.pdf
 
Higher Level Malware
Higher Level MalwareHigher Level Malware
Higher Level Malware
 
Blockchain and smart contracts, what they are and why you should really care ...
Blockchain and smart contracts, what they are and why you should really care ...Blockchain and smart contracts, what they are and why you should really care ...
Blockchain and smart contracts, what they are and why you should really care ...
 
Pentester++
Pentester++Pentester++
Pentester++
 
Not my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructureNot my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructure
 
The Final Frontier, Automating Dynamic Security Testing
The Final Frontier, Automating Dynamic Security TestingThe Final Frontier, Automating Dynamic Security Testing
The Final Frontier, Automating Dynamic Security Testing
 
ContainerDays Boston 2016: "Hiding in Plain Sight: Managing Secrets in a Cont...
ContainerDays Boston 2016: "Hiding in Plain Sight: Managing Secrets in a Cont...ContainerDays Boston 2016: "Hiding in Plain Sight: Managing Secrets in a Cont...
ContainerDays Boston 2016: "Hiding in Plain Sight: Managing Secrets in a Cont...
 
DEVIEW 2013
DEVIEW 2013DEVIEW 2013
DEVIEW 2013
 
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
 

Mais de Knoldus Inc.

Mais de Knoldus Inc. (20)

Authentication in Svelte using cookies.pptx
Authentication in Svelte using cookies.pptxAuthentication in Svelte using cookies.pptx
Authentication in Svelte using cookies.pptx
 
OAuth2 Implementation Presentation (Java)
OAuth2 Implementation Presentation (Java)OAuth2 Implementation Presentation (Java)
OAuth2 Implementation Presentation (Java)
 
Supply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptxSupply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptx
 
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML ParsingMastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
 
Akka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On IntroductionAkka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On Introduction
 
Entity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptxEntity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptx
 
Introduction to Redis and its features.pptx
Introduction to Redis and its features.pptxIntroduction to Redis and its features.pptx
Introduction to Redis and its features.pptx
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdf
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptx
 
Data Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingData Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable Testing
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose Kubernetes
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptx
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptx
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptx
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptx
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptx
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake Presentation
 
Scripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationScripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics Presentation
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIsGetting started with dotnet core Web APIs
Getting started with dotnet core Web APIs
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 

Último

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 

Rust Primer

  • 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
  • 7. Memory Safety ? Use After Free
  • 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.
  • 12. Mere pass Garbage Collector 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? 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
  • 20. Ownership Ownership Vijay does not have Maa now she is with Ravi now.
  • 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.
  • 31. Concurrency in Rust ● Threads ● Message Passing ● Shared State using Mutex ● Extensible Concurrency - Send and Sync Traits
  • 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.
  • 33. Cargo
  • 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.