Rust
Est. read time: 4 minutes | Last updated: December 17, 2024 by John Gentile
Contents
Why Rust?
Rust is relatively young compared to other systems programming languages. However, it has taken many of the lessons learned from decades of other languages to form something that:
- Puts memory safety at the forefront
- Allows for similar speed and zero-cost abstractions as C++
Why Not Rust?
Ignoring criticisms based on feelings and/or fanboy-isms, there has been some valid and well-researched opinions on where Rust may still fall short:
- Why Not Rust? - matklad
- Frustrated? It’s not you, it’s Rust - fasterthanlime
- Leaving Rust gamedev after 3 years - LogLog Games
Install and Getting Started
- Installing Rust is fairly simple using the rustup script:
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
- On macOS, make sure C compiler is installed with
$ xcode-select --install
- On macOS, make sure C compiler is installed with
- Rust can be updated at any time with
$ rustup update
Getting Started Resources
- rust-lang/rustlings - small exercises to get started with Rust
- The Rust Programming Language - rust-lang.org
- Rust Book Experiment: Rust book but w/interactive quizzes and highlighting.
- Rust By Example - rust-lang.org
- Rust Cookbook
- Rust Design Patterns
- Introduction - Learning Rust With Entirely Too Many Linked Lists
- idiomatic-rust: 🦀 A peer-reviewed collection of articles/talks/repos which teach concise, idiomatic Rust.
- Comprehensive Rust - Google Course
- From JavaScript to Rust
- ferrous-systems/elements-of-rust
- Ferrous Teaching Material
- Rust API Guidelines
- Advent of Rust: like Advent of Code but for beginners looking to learn Rust.
Books
- Zero to Production In Rust
- Programming Rust: Fast, Safe Systems Development 2nd Ed
- Rust for Rustaceans: see also the main website for errata.
- Rust Essentials - Second Edition
- Rust High Performance
Tools
Cargo
cargo
is installed automatically with the typical install process.
- A new Rust project can be created by running
$ cargo new <project_name>
and, by default, creates a variety of helpful files:- Creates a
.gitignore
file and -if not already within a git repo- initializes a new Git repo- To not have a Git repo instantiated, pass
--vcs none
.
- To not have a Git repo instantiated, pass
- A TOML config file
Cargo.toml
that describes the metadata and dependencies of the Rust project. - A
src/
directory in which Rust source should live. - Add
--lib
to the end ofcargo new
to create a library (no binary compilation target).
- Creates a
- When using a Cargo project,
$ cargo build
can be used$ cargo run
can be used to build and then execute in one command.- Or for a more straight forward approach,
$ rustc main.rs
compiles the Rust filemain.rs
into an executable filemain
.
For more info, see The Cargo Book.
As part of cargo
there’s also:
- clippy: a collection of lints to catch common mistakes and improve Rust code
- rustfmt: a Rust code formatter, can also be run with
cargo fmt
.
rustdoc
Rust ships with a tool called rustdoc which can generate documentation by parsing specially formatted comments in Rust source code.
rustdoc
can also perform documentation tests within comments.
Cross-Compile & FFI
- dtolnay/cxx: safe interop between Rust and C++.
Other Tools
- rust-analyzer: implements Language Server Protocol (LSP) for Rust (allows IDEs/editors to have completion, definition, goto, etc. features).
- cargo-audit: audits dependencies for crates with known security vulnerabilities, maintained by RustSec Advisory Database.
- cargo-deny: plugin for linting dependencies.
- grcov: collect & aggregate code coverage data for source files (also supports C/C++ projects, or files that can be processed by llvm/clang/gcc).
- loom: concurrency permutation testing for parallel Rust code.
- Miri: interpreter for Rust’s mid-level interpreter (MIR) and detect classes of undefined behavior.
Language
Macros
- tfpk/MacroKata: Learn Macros in Rust
- The Little Book of Rust Macros
Async
See rust-async-framework for more details and implementation examples.
References
- Rust Atomics and Locks: Low-Level Concurrency in Practice- Mara Bos
- Async Rust Is A Bad Language- Bit Bashing
- tokio-rs/tokio: runtime for async Rust apps
- Asynchronous Programming in Rust (Async-book)
- Pin and suffering - fasterthanlime
- The State of Async Rust: Runtimes
Rust for Performance
- Simply building with
--release
gives sane optimization defaults for most applications - Cheap tricks for high-performance Rust
- The Rust Performance Book
- Rust SIMD Performance Guide
Low-Level / Embedded
- Writing an OS in Rust - Philipp Oppermann
- Rust Embedded GitHub
- rust-embedded/rust-raspberrypi-OS-tutorials: learn to write an embedded OS in Rust
- The embedonomicon
- awesome-embedded-rust
- The Embedded Rustacean Newsletter
- Learn Rust the Dangerous Way
- High Assurance Rust
Rust for Linux Kernel Development
- Rust for Linux
- rust-sysfs-gpio
- Rust Kernel Module: Getting Started
- linux-kernel-module-rust
- https://github.com/lizhuohua/linux-kernel-module-rust
References
NOTE: offline documentation is locally installed with rustup
and can be opened in your browser by simply running rustup doc
. For public/open-source crates, documentation is also often found in Docs.rs.
Rust Core Docs
- The Standard Library: Comprehensive guide to the Rust standard library APIs.
- Edition Guide: Guide to the Rust editions.
- Cargo Book: A book on Rust’s package manager and build system.
- Rustdoc Book: Learn how to make awesome documentation for your crate.
- Rustc Book: Familiarize yourself with the knobs available in the Rust compiler.
- Compiler Error Index: In-depth explanations of the errors you may see from the Rust compiler.
- Rust Conf: Annual Rust conference.
Application Domain Docs and Repos
- Command Line Book
- clap: command line argument parser
- console-rs/indicatif: CLI progress bar/reporting library.
- tui-rs: Terminal User Interface (TUI) and dashboard crate
- Webassembly Book
- Embedded Rust Book
General Repos
Some repos that are useful in general Rust programs, or as reference for good Rust practices:
- rust-unofficial/awesome-rust: collection of neat Rust repos and material
- BurntSushi/ripgrep: very fast grep replacement, with great reference Rust codebase.
- alacritty: cross-platform, OpenGL terminal emulator.
- awslabs/mountpoint-s3: A simple, high-throughput file client for mounting an Amazon S3 bucket as a local file system.
- hashbrown: a Rust port of Google’s high-performance SwissTable hash map, adapted to make it a drop-in replacement for Rust’s standard HashMap and HashSet types.
- tracing: a framework for instrumenting Rust programs to collect structured, event-based diagnostic information.