about
Rust Track Summary
7/04/2024
Rust Track Summary
Story, Bites, and Repositories
Story Index
Bite Index
- Jorge Leitao on rust-users
-
Bits devoted to Rust
Small programs that illustrate HelloWorld, Data, Objects, Generics, and Iteration.
The fastest way to get a good mental model for Rust programming. -
Rust Story
An ebook with seven chapters that covers Rust programming at intermediate level. -
Rust Bites
A large collection of pages each focused on one feature of the Rust programming language. -
Blogs
Most of the recent blogs focus on Rust specifics, with fairly complete coverage of a single topic in each. -
Rust Playground code examples
Table of links to a collection of basic to intermediate code examples running in Rust Playground.
What I like about Rust
-
Performance - equivalent to C++
- native code, no garbage collection
- code optimization potential similar to C++
-
Rust vs C++ performance essentially equal
- Compare Rust and C++ message-passing systems
-
Safety - memory and data race safety by construction
-
Single ownership of resources with borrowing and transfer
- borrow by taking reference
-
transfer of owned resources for assignment and pass-by-value with a Move operation
Transfer does not apply to primitive types or aggregates of primitive types. Those are copied. It does apply to library types like Vecs and to most user defined types.
- previous owner cannot be used after transfer
- clones make copies explicitly, never silently
- no sharing of mutable data through references
-
no dangling pointers
- References must be initialized and are bound to only one referend throughout their lifetimes.
- Rust borrow checker ensures that no reference outlives its referend.
- It does this by checking, at compile time, scope-based lifetimes of both referend and reference.
-
cannot read or write to unowned memory
- Indexing off end of array causes panic which terminates current thread before any reads or writes can execute.
-
no data races in multi-threaded code
- failing to use lock is compile error
- mutex protects one specific datum, not a region of code
- compiler recognizes thread-safe and non-thread-safe types including user defined types
- RustBites_Safety
-
-
Code does exactly what its statements declare
-
Strict typing
- almost no silent conversions
- will dereference types that implement Deref trait
-
Very little syntax sugar
- doesn't need that due to clever type system
-
Very few context dependencies
- Compared to C++
-
Compiler error messages are very good
- often offers suggestions for fixing errors
-
-
Excellent code resources
-
Rust playground web app
- easy to test small code fragments
- playground
-
Standard Rust tool set
- rustc - comiler with excellent error messages
- cargo - build manager
- clippy - style verifier checks for idomatic code
- rustfmt - modifies code layout to match style standard
- rustdoc - generates docs from comments
-
std library - can view source code
-
Rust Community Open Source Crates
-
Visual Studio Code
- light weight IDE for Windows, Linux, macOS
- RustBites_Tooling
-
Code Examples
-
-
Very easy to get started and manage projects
-
up and running in a couple of hours
-
cargo build manager
-
Run cargo from command line in project directory
(where cargo.toml file resides) - cargo new <path name> [--lib]
- cargo run [--example <crate name without extension>]
- cargo clean
- cargo book
-
Run cargo from command line in project directory
- built-in test framework for libraries
-
style and construction advice from clippy
- cargo clippy
-
standard formatting with rustfmt
- cargo fmt
-
-
Excellent learning materials
Scan and pick a few that are right for you.-
Intermediate resources
-
Advanced resources
-
Documentation from Rust
-
Cheat sheets
-
Reputation of Rust as hard to learn is exaggerated
-
Compiler has borrow checker that makes builds fail when code may be unsafe.
While learning you will get a lot of these at first.
- Borrow checker is a friend at your elbow steering you toward correct safe code.
- Once code builds it is very likely to be correct.
- Cargo tool makes it very easy to run many quick tests on binaries and libraries.
-
Compiler has borrow checker that makes builds fail when code may be unsafe.
-
Other resources
Writing secure code in Rust
Mitre's Common Weakness Enumeration
Who uses Rust?Mozilla, Microsoft, Google, Amazon, Meta, Cloudflare, Cisco, Dropbox, Discord, ...A love letter to Rust from MIT Technology Review"There are 2.8 million coders writing in Rust, ..."
Table 1. Rust Resources from this site
Site Resources | Content |
---|---|
Rust Repositories | Index of all the Rust code repositories |
Rust Models | Summary of features with screenshots and examples - pdf |
Rust Story Index at upper right on this page. |
Rust ebook in 7 chapters first chapter in Rust Story |
Rust Bites Index at upper right on this page. |
Dozens of Code Bites about Rust first Rust Bite <-- good place to go after this page |
Rust Glossary | Definitions of common terms |
Rust FlashCards | Basic types and data structures |
Bits of Code | Compares small C++, Rust, C#, Python, and JavaScript codes |
Rust Playground | online compiler |
Code Examples | Content |
Rust Repositories | Index into Rust Repositories |
Rust Code examples | List of RustBites code examples, RustPlayground code examples, Rust Basic Demos Repository |
online code execution | RustPlayground, tutorialspoint |
Other Resources | Content |
Rust Guide | Definition and examples of Rust collections, iterators, and concurrency constructs. |
Tooling | Using Visual Studio Code to create and build Rust code |
Rust home site | Download and install, learn Rust |
Rust user's forum | Create an account and you can log-in, read, and post messages and questions. |
The Rust Reference | Semi-formal, but surprisingly readable, reference for Rust |
Rust API Guidelines | Guidelines for crafting Rust code, from the source |
Other intermediate and advanced resources | |
How to learn modern Rust | A collection of links to explorations of intermediate and advanced language materials |
idiomatic rust resources | Annotated links to articles and examples of community accepted styles and patterns for rust code. |
Exercises
- Write a Rust program to find the largest file in a specified directory tree, using walkdir crate from crates.io.
- Ammend Project #1 by accepting arguments from the command line, using args crate.
- Use ChatGPT to create a directory traversal crate and use that instead of walkdir.
- Write a Rust program to read a source code file and count the number of lines and number of scopes for each function. You can count scopes by counting open braces "{". Start by searhing for fn and then read each line searching for "{" and "}". Push each open brace on a stack, and pop when a closed brace is encountered. When the stack is empty, the end of the function has been reached.
- Modify Project #4 by counting lines and scopes for each file in a directory tree rooted at a specified folder, usually named "src".