SlideShare a Scribd company logo
1 of 22
L l o y d M o o r e , P r e s i d e n t
L l o y d @ C y b e r D a t a - R o b o t i c s . c o m
w w w . C y b e r D a t a - R o b o t i c s . c o m
A Slice of Rust
Agenda:
We are going to look at a collection of topics
related to Rust programming that I have found
interesting over the past year developing several
projects.
In some cases I’ll compare and contrast to C++
and in others we’ll just look at the Rust topic.
Rust Development Environment:
Quite complete development “out of the box”:
 Rustup: Tool chain management program, including cross compilation tools
 Cargo: Top level build process management tool and package management
 Rustfmt: Rust code formatter run with “cargo fmt”
 Clippy: Rust linter – run with “cargo clippy”
 Documents: Built in, generate docs with “cargo rustdoc” or “cargo doc”
 Unit tests: Built in, run with “cargo test”
 Performance testing: Built in, run with “cargo bench”
 Rust language server: Editor support for many major editors
 Vim
 Emacs
 Sublime Text
 Atom
 Visual Studio Code
 IntelliJ / CLion
Rust Development Cycle
 “Feels” VERY different from a C++ development cycle
 Basic mechanics are the same: Edit, Compile, Run
 Compiler messages will give suggestions as well as
outright errors
 Can also include code snip-its
 Result is you feel more like you are having a “dialog”
with the compiler to edit and build the code
Coding Style Idiom Based
 When coding in Rust look for existing
patterns that already solve your problem.
 Similar to Design Patterns they are much
smaller patterns to solve specific problems
 This is typically referred to as “idomatic
code” and is more important in Rust than
other languages
 In some cases the compiler and/or linter
(clippy) will actually enforce an idomatic
pattern
Rust Language Evolution
 No formal language specification!
 The compiler is the specification
 Does place limits on where the language can be
used – cannot use for some projects requiring
DO-178x – will not be able to fully qualify the tool
chain!
 Allows for faster language iteration
 Language is less “regular” than C++
 Has “Editions” for language breaking changes,
increment every 3 years so far
 Has “stable” and “nightly” compiler versions
 “Stable” compiler updated every 6 weeks!
Crates.io – The package registry
 Packages are called “crates” in Rust
 Registry is integrated with cargo for both package
download and version management
 Appears to be a rather blurry line between a
“standard” library and what ends up in the registry
 Most packages have a MIT or Apache style license
 Not “curated” in any way, watch for:
 Documentation quality
 Number of recent downloads – some crates are
defacto standards, ie: serde and rand
 Last release and release cadance
Crates.io – The Website
Crates.io – Typical Package Entry
Std::time::Duration vs chrono::Duration
 Lloyd’s rule: If there are two similar things Lloyd
has a 90% chance of finding the wrong one!
 Std::time::Duration and chrono::Duration do
basically the same thing but are not compatible.
 Chrono is the more feature rich package, and
seems to be usually what you want.
 Frequently trips me up though, and error
messages can be more confusing in the context of
two packages with similar constructs.
Borrow Checker
 Likely the most loved and hated feature of Rust!
 Enforces compile time checks on variables:
 Initialized before use
 Not moved twice
 Not moved while “borrowed”
 Cannot be modified by more than one owner at a
time
 Etc….
 Fundamentally responsible for enforcing the safety
guarantees that make Rust unique.
 Will FORCE you to use different and better
patterns when writing code!
Borrow Checker : Modifying a list while
iterating
This is a rather contrived example but shows the behavior of the borrow checker.
This check is done at compile time with ZERO run time overhead!
The error message is also quite helpful – once you get used to it.
Match Statement Efficiency
 Match statement is much like C++ switch
 Also includes much more advanced pattern
matching.
 Herb Sutter has proposed a similar construct for C++:
 https://www.youtube.com/watch?v=raB_289NxBk
 So just how efficient is this statement?
 Note that Rust currently uses the LLVM back end
compiler and benefits from the optimizations there.
Match Statement Efficiency
This was an interesting case I just happened to try.
A constant result evaluates to basically no code even without specifying
optimization.
Match Statement Efficiency
Looking at the first part of the assembly code you can see a range check and
then the calculations resulting in an indirect jump at line 8.
You do in fact get a jump table in this case.
Match Statement Efficiency
A shorter case however (only 0...2) results in a branch chain.
Apparently the compiler decided this construct was more efficient in this case.
 Overall Rust can be just as efficient as C++.
 If you consider that additional compile time checks
can replace run time checks Rust can be more
efficient than C++ in some cases.
 Rust pays for this with increased build times,
however I’ve found this no worse than using C++
with a linter, such as clang-tidy.
 Construction of code which does not copy values
around is more difficult than in C++ owing to
borrow checker restrictions.
General Rust Efficiency
No Inheritance
 Rust promotes the composition pattern over
inheritance.
 In practice I’ve found this does result in
either:
 Duplicated code as each object must
contain what would be in a base class
 Need to use a different object model
 Need to use generic functions
 Need to use macros to generate a “family”
of objects
Rust Macros
 Macros are much more important and robust
than C++.
 Macro substitution happens in the abstract
syntax tree – NOT textual substitution!
 Parameters in macros can be restricted to
specific syntactic elements (expression,
block, etc.)
 With this can build domain specific
languages with Rust macros that intermix
with normal Rust code.
Rust Macros
 Two types of Rust Macros
 macro_rules
 Conceptually like enhanced C++ macros
 Procedural macros – allows for creating full
syntax extensions, and run code at compile
time.
 Conceptually allow you convert one AST
into another AST
 Have not had a chance to try these yet,
but conceptually very powerful.
Resources
 Rust Home Page
 https://www.rust-lang.org
 The Little Book of Rust Books
 https://lborb.github.io/book/title-page.html
 Rust Language Cheat Sheet
 https://cheats.rs/#cargo
Questions?

More Related Content

Similar to A Slice Of Rust - A quick look at the Rust programming language

TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseSteve Reiner
 
Dotnet interview qa
Dotnet interview qaDotnet interview qa
Dotnet interview qaabcxyzqaz
 
Writing Rust Command Line Applications
Writing Rust Command Line ApplicationsWriting Rust Command Line Applications
Writing Rust Command Line ApplicationsAll Things Open
 
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 2016Codemotion
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworksYuri Visser
 
Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C SharpGanesh Samarthyam
 
Essential c notes singh projects
Essential c notes singh projectsEssential c notes singh projects
Essential c notes singh projectsSINGH PROJECTS
 
Code as Data workshop: Using source{d} Engine to extract insights from git re...
Code as Data workshop: Using source{d} Engine to extract insights from git re...Code as Data workshop: Using source{d} Engine to extract insights from git re...
Code as Data workshop: Using source{d} Engine to extract insights from git re...source{d}
 
Introduction to Git for developers
Introduction to Git for developersIntroduction to Git for developers
Introduction to Git for developersDmitry Guyvoronsky
 
Pulumi. Modern Infrastructure as Code.
Pulumi. Modern Infrastructure as Code.Pulumi. Modern Infrastructure as Code.
Pulumi. Modern Infrastructure as Code.Yurii Bychenok
 

Similar to A Slice Of Rust - A quick look at the Rust programming language (20)

TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San JoseTypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
TypeScript for Alfresco and CMIS - Alfresco DevCon 2012 San Jose
 
Dotnet interview qa
Dotnet interview qaDotnet interview qa
Dotnet interview qa
 
ewili13_submission_14
ewili13_submission_14ewili13_submission_14
ewili13_submission_14
 
Writing Rust Command Line Applications
Writing Rust Command Line ApplicationsWriting Rust Command Line Applications
Writing Rust Command Line Applications
 
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
 
Rust presentation convergeconf
Rust presentation convergeconfRust presentation convergeconf
Rust presentation convergeconf
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworks
 
Rust programming-language
Rust programming-languageRust programming-language
Rust programming-language
 
Modularity problems
Modularity  problemsModularity  problems
Modularity problems
 
Migrating From Cpp To C Sharp
Migrating From Cpp To C SharpMigrating From Cpp To C Sharp
Migrating From Cpp To C Sharp
 
Essential c
Essential cEssential c
Essential c
 
Essential c notes singh projects
Essential c notes singh projectsEssential c notes singh projects
Essential c notes singh projects
 
Essential c
Essential cEssential c
Essential c
 
Essential c
Essential cEssential c
Essential c
 
Essential c
Essential cEssential c
Essential c
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Code as Data workshop: Using source{d} Engine to extract insights from git re...
Code as Data workshop: Using source{d} Engine to extract insights from git re...Code as Data workshop: Using source{d} Engine to extract insights from git re...
Code as Data workshop: Using source{d} Engine to extract insights from git re...
 
Introduction to Git for developers
Introduction to Git for developersIntroduction to Git for developers
Introduction to Git for developers
 
Pulumi. Modern Infrastructure as Code.
Pulumi. Modern Infrastructure as Code.Pulumi. Modern Infrastructure as Code.
Pulumi. Modern Infrastructure as Code.
 
ruby pentest
ruby pentestruby pentest
ruby pentest
 

More from LloydMoore

Less Magical Numbers - A coding standard proposal
Less Magical Numbers - A coding standard proposalLess Magical Numbers - A coding standard proposal
Less Magical Numbers - A coding standard proposalLloydMoore
 
Debugging Intermittent Issues - A How To
Debugging Intermittent Issues - A How ToDebugging Intermittent Issues - A How To
Debugging Intermittent Issues - A How ToLloydMoore
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to considerLloydMoore
 
What Have We Lost - A look at some historical techniques
What Have We Lost - A look at some historical techniquesWhat Have We Lost - A look at some historical techniques
What Have We Lost - A look at some historical techniquesLloydMoore
 
Raspberry pi robotics
Raspberry pi roboticsRaspberry pi robotics
Raspberry pi roboticsLloydMoore
 
High Reliabilty Systems
High Reliabilty SystemsHigh Reliabilty Systems
High Reliabilty SystemsLloydMoore
 
Real Time Debugging - What to do when a breakpoint just won't do
Real Time Debugging - What to do when a breakpoint just won't doReal Time Debugging - What to do when a breakpoint just won't do
Real Time Debugging - What to do when a breakpoint just won't doLloydMoore
 
Using PSoC Creator
Using PSoC CreatorUsing PSoC Creator
Using PSoC CreatorLloydMoore
 
Using the Cypress PSoC Processor
Using the Cypress PSoC ProcessorUsing the Cypress PSoC Processor
Using the Cypress PSoC ProcessorLloydMoore
 
C for Microcontrollers
C for MicrocontrollersC for Microcontrollers
C for MicrocontrollersLloydMoore
 
Starting Raspberry Pi
Starting Raspberry PiStarting Raspberry Pi
Starting Raspberry PiLloydMoore
 

More from LloydMoore (12)

Less Magical Numbers - A coding standard proposal
Less Magical Numbers - A coding standard proposalLess Magical Numbers - A coding standard proposal
Less Magical Numbers - A coding standard proposal
 
Debugging Intermittent Issues - A How To
Debugging Intermittent Issues - A How ToDebugging Intermittent Issues - A How To
Debugging Intermittent Issues - A How To
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to consider
 
What Have We Lost - A look at some historical techniques
What Have We Lost - A look at some historical techniquesWhat Have We Lost - A look at some historical techniques
What Have We Lost - A look at some historical techniques
 
Raspberry pi robotics
Raspberry pi roboticsRaspberry pi robotics
Raspberry pi robotics
 
High Reliabilty Systems
High Reliabilty SystemsHigh Reliabilty Systems
High Reliabilty Systems
 
Real Time Debugging - What to do when a breakpoint just won't do
Real Time Debugging - What to do when a breakpoint just won't doReal Time Debugging - What to do when a breakpoint just won't do
Real Time Debugging - What to do when a breakpoint just won't do
 
PSoC USB HID
PSoC USB HIDPSoC USB HID
PSoC USB HID
 
Using PSoC Creator
Using PSoC CreatorUsing PSoC Creator
Using PSoC Creator
 
Using the Cypress PSoC Processor
Using the Cypress PSoC ProcessorUsing the Cypress PSoC Processor
Using the Cypress PSoC Processor
 
C for Microcontrollers
C for MicrocontrollersC for Microcontrollers
C for Microcontrollers
 
Starting Raspberry Pi
Starting Raspberry PiStarting Raspberry Pi
Starting Raspberry Pi
 

Recently uploaded

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

A Slice Of Rust - A quick look at the Rust programming language

  • 1. L l o y d M o o r e , P r e s i d e n t L l o y d @ C y b e r D a t a - R o b o t i c s . c o m w w w . C y b e r D a t a - R o b o t i c s . c o m A Slice of Rust
  • 2. Agenda: We are going to look at a collection of topics related to Rust programming that I have found interesting over the past year developing several projects. In some cases I’ll compare and contrast to C++ and in others we’ll just look at the Rust topic.
  • 3. Rust Development Environment: Quite complete development “out of the box”:  Rustup: Tool chain management program, including cross compilation tools  Cargo: Top level build process management tool and package management  Rustfmt: Rust code formatter run with “cargo fmt”  Clippy: Rust linter – run with “cargo clippy”  Documents: Built in, generate docs with “cargo rustdoc” or “cargo doc”  Unit tests: Built in, run with “cargo test”  Performance testing: Built in, run with “cargo bench”  Rust language server: Editor support for many major editors  Vim  Emacs  Sublime Text  Atom  Visual Studio Code  IntelliJ / CLion
  • 4. Rust Development Cycle  “Feels” VERY different from a C++ development cycle  Basic mechanics are the same: Edit, Compile, Run  Compiler messages will give suggestions as well as outright errors  Can also include code snip-its  Result is you feel more like you are having a “dialog” with the compiler to edit and build the code
  • 5. Coding Style Idiom Based  When coding in Rust look for existing patterns that already solve your problem.  Similar to Design Patterns they are much smaller patterns to solve specific problems  This is typically referred to as “idomatic code” and is more important in Rust than other languages  In some cases the compiler and/or linter (clippy) will actually enforce an idomatic pattern
  • 6. Rust Language Evolution  No formal language specification!  The compiler is the specification  Does place limits on where the language can be used – cannot use for some projects requiring DO-178x – will not be able to fully qualify the tool chain!  Allows for faster language iteration  Language is less “regular” than C++  Has “Editions” for language breaking changes, increment every 3 years so far  Has “stable” and “nightly” compiler versions  “Stable” compiler updated every 6 weeks!
  • 7. Crates.io – The package registry  Packages are called “crates” in Rust  Registry is integrated with cargo for both package download and version management  Appears to be a rather blurry line between a “standard” library and what ends up in the registry  Most packages have a MIT or Apache style license  Not “curated” in any way, watch for:  Documentation quality  Number of recent downloads – some crates are defacto standards, ie: serde and rand  Last release and release cadance
  • 9. Crates.io – Typical Package Entry
  • 10. Std::time::Duration vs chrono::Duration  Lloyd’s rule: If there are two similar things Lloyd has a 90% chance of finding the wrong one!  Std::time::Duration and chrono::Duration do basically the same thing but are not compatible.  Chrono is the more feature rich package, and seems to be usually what you want.  Frequently trips me up though, and error messages can be more confusing in the context of two packages with similar constructs.
  • 11. Borrow Checker  Likely the most loved and hated feature of Rust!  Enforces compile time checks on variables:  Initialized before use  Not moved twice  Not moved while “borrowed”  Cannot be modified by more than one owner at a time  Etc….  Fundamentally responsible for enforcing the safety guarantees that make Rust unique.  Will FORCE you to use different and better patterns when writing code!
  • 12. Borrow Checker : Modifying a list while iterating This is a rather contrived example but shows the behavior of the borrow checker. This check is done at compile time with ZERO run time overhead! The error message is also quite helpful – once you get used to it.
  • 13. Match Statement Efficiency  Match statement is much like C++ switch  Also includes much more advanced pattern matching.  Herb Sutter has proposed a similar construct for C++:  https://www.youtube.com/watch?v=raB_289NxBk  So just how efficient is this statement?  Note that Rust currently uses the LLVM back end compiler and benefits from the optimizations there.
  • 14. Match Statement Efficiency This was an interesting case I just happened to try. A constant result evaluates to basically no code even without specifying optimization.
  • 15. Match Statement Efficiency Looking at the first part of the assembly code you can see a range check and then the calculations resulting in an indirect jump at line 8. You do in fact get a jump table in this case.
  • 16. Match Statement Efficiency A shorter case however (only 0...2) results in a branch chain. Apparently the compiler decided this construct was more efficient in this case.
  • 17.  Overall Rust can be just as efficient as C++.  If you consider that additional compile time checks can replace run time checks Rust can be more efficient than C++ in some cases.  Rust pays for this with increased build times, however I’ve found this no worse than using C++ with a linter, such as clang-tidy.  Construction of code which does not copy values around is more difficult than in C++ owing to borrow checker restrictions. General Rust Efficiency
  • 18. No Inheritance  Rust promotes the composition pattern over inheritance.  In practice I’ve found this does result in either:  Duplicated code as each object must contain what would be in a base class  Need to use a different object model  Need to use generic functions  Need to use macros to generate a “family” of objects
  • 19. Rust Macros  Macros are much more important and robust than C++.  Macro substitution happens in the abstract syntax tree – NOT textual substitution!  Parameters in macros can be restricted to specific syntactic elements (expression, block, etc.)  With this can build domain specific languages with Rust macros that intermix with normal Rust code.
  • 20. Rust Macros  Two types of Rust Macros  macro_rules  Conceptually like enhanced C++ macros  Procedural macros – allows for creating full syntax extensions, and run code at compile time.  Conceptually allow you convert one AST into another AST  Have not had a chance to try these yet, but conceptually very powerful.
  • 21. Resources  Rust Home Page  https://www.rust-lang.org  The Little Book of Rust Books  https://lborb.github.io/book/title-page.html  Rust Language Cheat Sheet  https://cheats.rs/#cargo