Rust
Est. read time: 3 minutes | Last updated: July 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 Tools
- 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
Compilation
- If 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
.
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
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
Async
See rust-async-framework for more details and implementation examples.
References
- tokio-rs/tokio: runtime for async Rust apps
- Asynchronous Programming in Rust
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
- Learn Rust the Dangerous Way
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
Application Domain Docs
To Read
- The Rust Programming Language - rust-lang.org
- Rust Book Experiment: Rust book but w/interactive quizzes and highlighting.
- Rust By Example - rust-lang.org
- Comprehensive Rust - Google Course
- From JavaScript to Rust
- The Rust Performance Book
- rust-lang/rustlings - small exercises to get started with Rust
- Rust API Guidelines
- ferrous-systems/elements-of-rust
- Ferrous Teaching Material
- Advent of Code - 2022
- High Assurance Rust
Books
- Zero to Production In Rust
- Programming Rust: Fast, Safe Systems Development 2nd Ed
- Rust for Rustaceans
Repos
- rust-unofficial/awesome-rust: collection of neat Rust repos and material
- BurntSushi/ripgrep: very fast grep replacement, with great reference Rust codebase.