Skip to content

Latest commit

 

History

History
265 lines (243 loc) · 21.5 KB

intro.md

File metadata and controls

265 lines (243 loc) · 21.5 KB

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.

Recipe Crates Categories
Read lines of strings from a file [![std-badge]][std] [![cat-filesystem-badge]][cat-filesystem]
Read and write integers in little-endian byte order [![byteorder-badge]][byteorder] [![cat-encoding-badge]][cat-encoding]
Generate random numbers [![rand-badge]][rand] [![cat-science-badge]][cat-science]
Generate random numbers within a range [![rand-badge]][rand] [![cat-science-badge]][cat-science]
Generate random numbers with given distribution [![rand-badge]][rand] [![cat-science-badge]][cat-science]
Generate random values of a custom type [![rand-badge]][rand] [![cat-science-badge]][cat-science]
Create random passwords from a set of alphanumeric characters [![rand-badge]][rand] [![cat-os-badge]][cat-os]
Create random passwords from a set of user-defined characters [![rand-badge]][rand] [![cat-os-badge]][cat-os]
Run an external command and process stdout [![regex-badge]][regex] [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing]
Run an external command passing it stdin and check for an error code [![std-badge]][std] [![cat-os-badge]][cat-os]
Run piped external commands [![std-badge]][std] [![cat-os-badge]][cat-os]
Redirect both stdout and stderr of child process to the same file [![std-badge]][std] [![cat-os-badge]][cat-os]
Continuously process child process' outputs [![std-badge]][std] [![cat-os-badge]][cat-os][![cat-text-processing-badge]][cat-text-processing]
Filter a log file by matching multiple regular expressions [![regex-badge]][regex] [![cat-text-processing-badge]][cat-text-processing]
Declare lazily evaluated constant [![lazy_static-badge]][lazy_static] [![cat-caching-badge]][cat-caching] [![cat-rust-patterns-badge]][cat-rust-patterns]
Maintain global mutable state [![lazy_static-badge]][lazy_static] [![cat-rust-patterns-badge]][cat-rust-patterns]
Verify and extract login from an email address [![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
Extract a list of unique #Hashtags from a text [![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
Replace all occurrences of one text pattern with another pattern. [![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
Extract phone numbers from text [![regex-badge]][regex] [![cat-text-processing-badge]][cat-text-processing]
Calculate the SHA-256 digest of a file [![ring-badge]][ring] [![data-encoding-badge]][data-encoding] [![cat-cryptography-badge]][cat-cryptography]
Sign and verify a message with an HMAC digest [![ring-badge]][ring] [![cat-cryptography-badge]][cat-cryptography]
Salt and hash a password with PBKDF2 [![ring-badge]][ring] [![data-encoding-badge]][data-encoding] [![cat-cryptography-badge]][cat-cryptography]
Define and operate on a type represented as a bitfield [![bitflags-badge]][bitflags] [![cat-no-std-badge]][cat-no-std]
Access a file randomly using a memory map [![memmap-badge]][memmap] [![cat-filesystem-badge]][cat-filesystem]
Check number of logical cpu cores [![num_cpus-badge]][num_cpus] [![cat-hardware-support-badge]][cat-hardware-support]
Avoid discarding errors during error conversions [![error-chain-badge]][error-chain] [![cat-rust-patterns-badge]][cat-rust-patterns]
Obtain backtrace of complex error scenarios [![error-chain-badge]][error-chain] [![cat-rust-patterns-badge]][cat-rust-patterns]
Measure elapsed time [![std-badge]][std] [![cat-time-badge]][cat-time]
Convert date to UNIX timestamp and vice versa [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Convert a local time to an another UTC timezone and vice versa [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Display formatted date and time [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Parse string into DateTime struct [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Perform checked date and time calculations [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Examine the date and time [![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]
Recipe Crates Categories
Serialize and deserialize unstructured JSON [![serde-json-badge]][serde-json] [![cat-encoding-badge]][cat-encoding]
Deserialize a TOML configuration file [![toml-badge]][toml] [![cat-encoding-badge]][cat-encoding]
Percent-encode a string [![url-badge]][url] [![cat-encoding-badge]][cat-encoding]
Encode a string as application/x-www-form-urlencoded [![url-badge]][url] [![cat-encoding-badge]][cat-encoding]
Encode and decode hex [![data-encoding-badge]][data-encoding] [![cat-encoding-badge]][cat-encoding]
Encode and decode base64 [![base64-badge]][base64] [![cat-encoding-badge]][cat-encoding]
Read CSV records [![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
Read CSV records with different delimiter [![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
Filter CSV records matching a predicate [![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
Handle invalid CSV data with Serde [![csv-badge]][csv] [![serde-badge]][serde] [![cat-encoding-badge]][cat-encoding]
Serialize records to CSV [![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
Serialize records to CSV using Serde [![csv-badge]][csv] [![serde-badge]][serde] [![cat-encoding-badge]][cat-encoding]
Recipe Crates Categories
Mutate the elements of an array in parallel [![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
Test in parallel if any or all elements of a collection match a given predicate [![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
Search items using given predicate in parallel [![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
Sort a vector in parallel [![rayon-badge]][rayon] [![rand-badge]][rand] [![cat-concurrency-badge]][cat-concurrency]
Map-reduce in parallel [![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
Generate jpg thumbnails in parallel [![rayon-badge]][rayon] [![glob-badge]][glob] [![image-badge]][image] [![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem]
Spawn a short-lived thread [![crossbeam-badge]][crossbeam] [![cat-concurrency-badge]][cat-concurrency]
Draw fractal dispatching work to a thread pool [![threadpool-badge]][threadpool] [![num-badge]][num] [![num_cpus-badge]][num_cpus] [![image-badge]][image] [![cat-concurrency-badge]][cat-concurrency][![cat-science-badge]][cat-science][![cat-rendering-badge]][cat-rendering]
Calculate SHA1 sum of *.iso files concurrently [![threadpool-badge]][threadpool] [![walkdir-badge]][walkdir] [![num_cpus-badge]][num_cpus] [![ring-badge]][ring] [![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem]
Recipe Crates Categories
Parse a URL from a string to a Url type [![url-badge]][url] [![cat-net-badge]][cat-net]
Create a base URL by removing path segments [![url-badge]][url] [![cat-net-badge]][cat-net]
Create new URLs from a base URL [![url-badge]][url] [![cat-net-badge]][cat-net]
Extract the URL origin (scheme / host / port) [![url-badge]][url] [![cat-net-badge]][cat-net]
Remove fragment identifiers and query pairs from a URL [![url-badge]][url] [![cat-net-badge]][cat-net]
Make a HTTP GET request [![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
Download a file to a temporary directory [![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem]
Query the GitHub API [![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]
Consume a paginated RESTful API [![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]
Check if an API resource exists [![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
Set custom headers and URL parameters for a REST request [![reqwest-badge]][reqwest] [![hyper-badge]][hyper] [![url-badge]][url] [![cat-net-badge]][cat-net]
Create and delete Gist with GitHub API [![reqwest-badge]][reqwest] [![serde-badge]][serde] [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding]
POST a file to paste-rs [![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
Listen on unused port TCP/IP [![std-badge]][std] [![cat-net-badge]][cat-net]
Extract all links from a webpage HTML [![reqwest-badge]][reqwest] [![select-badge]][select] [![cat-net-badge]][cat-net]
Check webpage for broken links [![reqwest-badge]][reqwest] [![select-badge]][select] [![url-badge]][url] [![cat-net-badge]][cat-net]
Extract all unique links from a MediaWiki markup [![reqwest-badge]][reqwest] [![regex-badge]][regex] [![cat-net-badge]][cat-net]
Make a partial download with HTTP range headers [![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
Recipe Crates Categories
Parse command line arguments [![clap-badge]][clap] [![cat-command-line-badge]][cat-command-line]
Decompress a tarball [![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]
Compress a directory into a tarball [![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]
Decompress a tarball while removing a prefix from the paths [![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]
Avoid writing and reading from a same file [![same_file-badge]][same_file] [![cat-filesystem-badge]][cat-filesystem]
Find loops for a given path [![same_file-badge]][same_file] [![cat-filesystem-badge]][cat-filesystem]
Recursively find duplicate file names [![walkdir-badge]][walkdir] [![cat-filesystem-badge]][cat-filesystem]
Recursively find all files with given predicate [![walkdir-badge]][walkdir] [![cat-filesystem-badge]][cat-filesystem]
Traverse directories while skipping dotfiles [![walkdir-badge]][walkdir] [![cat-filesystem-badge]][cat-filesystem]
Recursively calculate file sizes at given depth [![walkdir-badge]][walkdir] [![cat-filesystem-badge]][cat-filesystem]
Find all png files recursively [![glob-badge]][glob] [![cat-filesystem-badge]][cat-filesystem]
Find all files with given pattern ignoring filename case [![glob-badge]][glob] [![cat-filesystem-badge]][cat-filesystem]
Parse and increment a version string [![semver-badge]][semver] [![cat-config-badge]][cat-config]
Parse a complex version string [![semver-badge]][semver] [![cat-config-badge]][cat-config]
Check if given version is pre-release [![semver-badge]][semver] [![cat-config-badge]][cat-config]
Find the latest version satisfying given range [![semver-badge]][semver] [![cat-config-badge]][cat-config]
Check external command version for compatibility [![semver-badge]][semver] [![cat-text-processing-badge]][cat-text-processing] [![cat-os-badge]][cat-os]
Recipe Crates Categories
Log a debug message to the console [![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
Log an error message to the console [![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
Enable log levels per module [![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
Log to stdout instead of stderr [![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
Log messages with a custom logger [![log-badge]][log] [![cat-debugging-badge]][cat-debugging]
Use a custom environment variable to set up logging [![log-badge]][log] [![env_logger-badge]][env_logger] [![cat-debugging-badge]][cat-debugging]
Include timestamp in log messages [![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] [![cat-debugging-badge]][cat-debugging]
Log to the Unix syslog [![log-badge]][log] [![syslog-badge]][syslog] [![cat-debugging-badge]][cat-debugging]
Log messages to a custom location [![log-badge]][log] [![log4rs-badge]][log4rs] [![cat-debugging-badge]][cat-debugging]
Recipe Crates Categories
Compile and link statically to a bundled C library [![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
Compile and link statically to a bundled C++ library [![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
Compile a C library while setting custom defines [![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]

{{#include links.md}}