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 to way get involved with the Rust community. It needs and welcomes help. For details see CONTRIBUTING.md.

Basics

Recipe Crates Categories
Read lines of strings from a file std-badge cat-filesystem-badge
Read and write integers in little-endian byte order byteorder-badge cat-encoding-badge
Generate random floating point numbers rand-badge cat-science-badge
Generate random numbers within a range rand-badge cat-science-badge
Generate random numbers with normal distribution rand-badge cat-science-badge
Generate random values of a custom type rand-badge cat-science-badge
Run an external command and process stdout regex-badge cat-os-badge cat-text-processing-badge
Declare lazily evaluated constant lazy_static-badge cat-caching-badge cat-rust-patterns-badge
Maintain global mutable state lazy_static-badge cat-rust-patterns-badge
Access a file randomly using a memory map memmap-badge cat-filesystem-badge
Define and operate on a type represented as a bitfield bitflags-badge cat-no-std-badge

Encoding

Recipe Crates Categories
Serialize and deserialize unstructured JSON serde-json-badge cat-encoding-badge
Deserialize a TOML configuration file toml-badge cat-encoding-badge
Percent-encode a string url-badge cat-encoding-badge
Encode a string as application/x-www-form-urlencoded url-badge cat-encoding-badge
Encode and decode hex data-encoding-badge cat-encoding-badge
Encode and decode base64 base64-badge cat-encoding-badge

Concurrency

Recipe Crates Categories
Mutate the elements of an array in parallel rayon-badge cat-concurrency-badge
Spawn a short-lived thread crossbeam-badge cat-concurrency-badge
Draw fractal dispatching work to a thread pool threadpool-badge num-badge num_cpus-badge image-badge cat-concurrency-badgecat-science-badgecat-rendering-badge

Networking

Recipe Crates Categories
Parse a URL from a string to a Url type url-badge cat-net-badge
Create a base URL by removing path segments url-badge cat-net-badge
Create new URLs from a base URL url-badge cat-net-badge
Extract the URL origin (scheme / host / port) url-badge cat-net-badge
Remove fragment identifiers and query pairs from a URL url-badge cat-net-badge
Make a HTTP GET request after parsing a URL reqwest-badge cat-net-badge
Download a file to a temporary directory reqwest-badge tempdir-badge cat-net-badge cat-filesystem-badge
Query the GitHub API reqwest-badge serde-badge cat-net-badge cat-encoding-badge
Consume a paginated RESTful API reqwest-badge serde-badge cat-net-badge cat-encoding-badge
Check if an API resource exists reqwest-badge cat-net-badge
Set custom headers and URL parameters for a REST request reqwest-badge hyper-badge url-badge cat-net-badge
Create and delete Gist with GitHub API reqwest-badge serde-badge cat-net-badge cat-encoding-badge
POST a file to paste-rs reqwest-badge cat-net-badge
Listen on unused port TCP/IP std-badge cat-net-badge
Extract all links from a webpage reqwest-badge select-badge cat-net-badge

Application development

Recipe Crates Categories
Parse command line arguments clap-badge cat-command-line-badge
Decompress a tarball flate2-badge tar-badge cat-compression-badge
Compress a directory into a tarball flate2-badge tar-badge cat-compression-badge
Decompress a tarball while removing a prefix from the paths flate2-badge tar-badge cat-compression-badge
Find loops for a given path same_file-badge cat-filesystem-badge
Recursively find duplicate file names walkdir-badge cat-filesystem-badge
Recursively find all files with given predicate walkdir-badge cat-filesystem-badge
Traverse directories while skipping dotfiles walkdir-badge cat-filesystem-badge
Recursively calculate file sizes at given depth walkdir-badge cat-filesystem-badge
Find all png files recursively glob-badge cat-filesystem-badge
Find all files with given pattern ignoring filename case glob-badge cat-filesystem-badge

Logging

Recipe Crates Categories
Log a debug message to the console log-badge env_logger-badge cat-debugging-badge
Log an error message to the console log-badge env_logger-badge cat-debugging-badge
Enable log levels per module log-badge env_logger-badge cat-debugging-badge
Log to stdout instead of stderr log-badge env_logger-badge cat-debugging-badge
Log messages with a custom logger log-badge cat-debugging-badge
Include timestamp in log messages log-badge env_logger-badge chrono-badge cat-debugging-badge
Log to the Unix syslog log-badge syslog-badge cat-debugging-badge
Log messages to a custom location log-badge cat-debugging-badge