Reference · 18 entries
Rust Standard Library Reference
A Rust cheat sheet: Option and Result methods, the ? operator, strings, Vec and HashMap, iterators, derive, Display, From and Into, and smart pointers, with real examples compiled with Rust 1.99.
Rust 1.99 (2024 edition) Version history
Every example is a complete program that was compiled with Rust 1.99 (2024 edition) and run when this page was built, so the output is real. For explanations, see the Rust tutorial.
Option and Result
Handle missing values and errors: unwrap_or, map, and_then, the ? operator, map_err and collecting Results.
| Name | Description |
|---|---|
Option::unwrap_or and friends | Get the value out of an Option with a default, a computed default, or the type's default instead of panicking with unwrap. |
Option::map, and_then and filter | Transform the value inside an Option without unwrapping it, chain optional steps, or keep it only if a condition holds. |
The ? operator | Propagate errors from functions returning Result or Option: unwrap Ok values and return early on Err. |
Result::map_err, unwrap_or_else and ok | Convert or recover from errors: change the error type, fall back to a value, or discard the error as an Option. |
Strings
Work with String and &str: trimming, splitting, searching, replacing, building and iterating characters.
| Name | Description |
|---|---|
String and &str methods | Common text operations: trim, split, contains, replace, case conversion, starts_with and parse. |
String::push_str, format! and join | Build strings: append with push_str and push, format with format!, and join a list of strings with a separator. |
str::chars and char_indices | Iterate over the Unicode characters of a string (not bytes), with their byte positions when needed. |
Vec and HashMap
Grow, sort, search and filter vectors, and insert, update, look up and iterate maps.
| Name | Description |
|---|---|
Vec: push, pop, retain, dedup and sort | Grow and shrink vectors, remove items by condition, deduplicate, and sort, including by key or float values. |
slice contains, iter().position and binary_search | Search slices: membership, the index of a match, the first matching item, or a binary search in sorted data. |
HashMap::entry | Insert or update a map value in one lookup: count occurrences, append to lists, or insert a default if missing. |
HashMap get, insert, remove and iteration | Look up, add and remove map entries, and iterate in a deterministic order by sorting keys or using BTreeMap. |
Iterators
Transform, filter, reduce and collect iterators: map, filter_map, sum, fold, max_by_key and collect.
| Name | Description |
|---|---|
Iterator::map, filter and filter_map | Transform and select items lazily: map each item, keep matching ones, or do both at once with filter_map. |
Iterator::sum, fold, any and max_by_key | Reduce an iterator to one value: totals, counts, custom folds, checks, and minimum or maximum items. |
Iterator::collect | Turn an iterator into a collection: Vec, String, HashMap, HashSet, or a Result that fails on the first error. |
Traits and Smart Pointers
Derive standard traits, implement Display and From, and share data with Box, Rc, RefCell and Arc.
| Name | Description |
|---|---|
#[derive] | Generate standard trait implementations such as Debug, Clone, PartialEq, Eq, Hash, Default and PartialOrd for your types. |
impl Display | Implement std::fmt::Display to control how a type prints with {} and converts with to_string(). |
From and Into | Convert between types: implement From<T> and get Into for free; ? uses From to convert error types. |
Box, Rc, RefCell and Arc | Choose the right smart pointer: heap allocation, shared ownership, interior mutability, and thread-safe sharing. |