Cookin’ with Rust
This Rust Cookbook is a collection of simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem.
Read more about Rust Cookbook, including tips for how to read the book, how to use the examples, and notes on conventions.
Contributing
This project is intended to be easy for new Rust programmers to contribute to, and an easy way to get involved with the Rust community. It needs and welcomes help. For details see CONTRIBUTING.md.
Algorithms
Asynchronous
Command Line
| Recipe | Crates | Categories |
|---|---|---|
| Parse command line arguments | ||
| ANSI Terminal |
Compression
| Recipe | Crates | Categories |
|---|---|---|
| Decompress a tarball | ||
| Compress a directory into a tarball | ||
| Decompress a tarball while removing a prefix from the paths |
Concurrency
Cryptography
| Recipe | Crates | Categories |
|---|---|---|
| Calculate the SHA-256 digest of a file | ||
| Sign and verify a message with an HMAC digest | ||
| Salt and hash a password with PBKDF2 |
Data Structures
Database
| Recipe | Crates | Categories |
|---|---|---|
| Create a SQLite database | ||
| Insert and Query data | ||
| Create tables in a Postgres database | ||
| Insert and Query data | ||
| Aggregate data |
Date and Time
Development Tools
Debugging
Versioning
Build Time
| Recipe | Crates | Categories |
|---|---|---|
| Compile and link statically to a bundled C library | ||
| Compile and link statically to a bundled C++ library | ||
| Compile a C library while setting custom defines |
Encoding
File System
Hardware Support
| Recipe | Crates | Categories |
|---|---|---|
| Check number of logical cpu cores |
Memory Management
| Recipe | Crates | Categories |
|---|---|---|
| Declare lazily evaluated constant | ||
| Std::cell | ||
std::cell:LazyCell | ||
std::sync::LazyLock |
Networking
| Recipe | Crates | Categories |
|---|---|---|
| Listen on unused port TCP/IP |
Operating System
Science
science/mathematics
Text Processing
WebAssembly
wasmtime is an embeddable WebAssembly runtime. These recipes
cover the host embedding API — loading modules, calling exports, sharing
linear memory, and wiring up host-defined functions that guests can call back
into.
The recipes live outside the main workspace because wasmtime is a large
dependency not used elsewhere. Run them with
cargo run --manifest-path crates/wasm/Cargo.toml --bin <name>.
Embedding wasmtime
| Recipe | Crates | Categories |
|---|---|---|
| Call an exported WebAssembly function | ||
| Exchange data via WebAssembly linear memory | ||
| Define host functions for WebAssembly | ||
| Component Model — typed strings and structs |
Web Programming
Scraping Web Pages
| Recipe | Crates | Categories |
|---|---|---|
| Extract all links from a webpage HTML | ||
| Check webpage for broken links | ||
| Extract all unique links from a MediaWiki markup |
Uniform Resource Locations (URL)
Media Types (MIME)
| Recipe | Crates | Categories |
|---|---|---|
| Get MIME type from string | ||
| Get MIME type from filename | ||
| Parse the MIME type of a HTTP response |
Clients
Web Authentication
| Recipe | Crates | Categories |
|---|---|---|
| Basic Authentication |
Full Stack Web
| Recipe | Crates | Categories |
|---|---|---|
| Return filtered results as HTML | ||
| Synchronize component state with the server |