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.

NameDescription
Option::unwrap_or and friendsGet 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 filterTransform the value inside an Option without unwrapping it, chain optional steps, or keep it only if a condition holds.
The ? operatorPropagate errors from functions returning Result or Option: unwrap Ok values and return early on Err.
Result::map_err, unwrap_or_else and okConvert 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.

NameDescription
String and &str methodsCommon text operations: trim, split, contains, replace, case conversion, starts_with and parse.
String::push_str, format! and joinBuild strings: append with push_str and push, format with format!, and join a list of strings with a separator.
str::chars and char_indicesIterate 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.

NameDescription
Vec: push, pop, retain, dedup and sortGrow and shrink vectors, remove items by condition, deduplicate, and sort, including by key or float values.
slice contains, iter().position and binary_searchSearch slices: membership, the index of a match, the first matching item, or a binary search in sorted data.
HashMap::entryInsert or update a map value in one lookup: count occurrences, append to lists, or insert a default if missing.
HashMap get, insert, remove and iterationLook 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.

NameDescription
Iterator::map, filter and filter_mapTransform 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_keyReduce an iterator to one value: totals, counts, custom folds, checks, and minimum or maximum items.
Iterator::collectTurn 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.

NameDescription
#[derive]Generate standard trait implementations such as Debug, Clone, PartialEq, Eq, Hash, Default and PartialOrd for your types.
impl DisplayImplement std::fmt::Display to control how a type prints with {} and converts with to_string().
From and IntoConvert between types: implement From<T> and get Into for free; ? uses From to convert error types.
Box, Rc, RefCell and ArcChoose the right smart pointer: heap allocation, shared ownership, interior mutability, and thread-safe sharing.

Was this page helpful?