The standard library comes with the compiler. It is split into three crates, so that code for systems with no operating system can use the parts that do not need one. This page is a short guide; the full reference is the std documentation.
core, alloc and std
| Crate | Needs | Provides |
|---|---|---|
core | Nothing | Primitive types’ methods, Option, Result, iterators, fmt, cells, atomics, the marker and operator traits, Future |
alloc | A global allocator | Box, Vec, String, Rc, Arc, BTreeMap and other heap types |
std | An operating system | Everything in core and alloc, plus fs, io, net, thread, process, env, time, HashMap |
std re-exports the items of core and alloc, so
std::option::Option and core::option::Option are the same.
#![no_std] links core instead of std; such a
crate can still use alloc with extern crate alloc;. A
no_std binary must supply its own
panic handler.
The prelude
The prelude is the set of names available in every module without a
use. It depends on the edition.
| Prelude | Contents |
|---|---|
std::prelude::v1 | Copy, Send, Sized, Sync, Unpin, Fn, FnMut, FnOnce, AsyncFn, AsyncFnMut, AsyncFnOnce, Drop, drop, size_of, size_of_val, align_of, align_of_val, Box, ToOwned, Clone, PartialEq, PartialOrd, Eq, Ord, AsRef, AsMut, Into, From, Default, Iterator, Extend, IntoIterator, DoubleEndedIterator, ExactSizeIterator, Option with Some and None, Result with Ok and Err, String, ToString, Vec, and most standard macros |
rust_2015, rust_2018 | v1 unchanged |
rust_2021 | v1, plus TryFrom, TryInto, FromIterator |
rust_2024 | rust_2021, plus Future, IntoFuture |
#![no_implicit_prelude] disables the prelude in a module.
Modules
| Area | Modules |
|---|---|
| Language support | marker, ops, cmp, clone, default, convert, mem, ptr, any, hint, arch, intrinsics (unstable), primitive |
| Data | option, result, array, slice, str, string, char, ascii, vec, boxed, collections, num, f32, f64 |
| Iteration and text | iter, fmt, borrow, hash |
| Memory and sharing | alloc, cell, rc, sync, pin |
| Errors and panics | error, panic, backtrace |
| Concurrency | thread, sync, future, task |
| Operating system | env, fs, io, net, os, path, process, time, ffi |
Collections and strings
| Type | Purpose |
|---|---|
Vec<T> | A growable array |
VecDeque<T> | A double-ended queue |
LinkedList<T> | A doubly linked list |
HashMap<K, V>, HashSet<T> | Hash table map and set (in std) |
BTreeMap<K, V>, BTreeSet<T> | Ordered map and set |
BinaryHeap<T> | A priority queue |
String | A growable, heap-allocated UTF-8 string; &str is a borrowed slice of one |
OsString, OsStr, PathBuf, Path | Strings and paths in the operating system’s encoding |
CString, CStr | C-style null-terminated strings |
Common traits
| Trait | Use |
|---|---|
Copy, Clone | Duplication |
Drop | Destructors |
Default | A default value |
PartialEq, Eq, PartialOrd, Ord, Hash | Comparison and hashing |
From, Into, TryFrom, TryInto, AsRef, AsMut | Conversions |
Iterator, IntoIterator, FromIterator, Extend | Iteration |
Display, Debug, LowerHex, UpperHex, Binary, Octal, LowerExp, UpperExp, Pointer | Formatting through fmt |
Read, Write, BufRead, Seek | Input and output in std::io |
Deref, DerefMut, Index, IndexMut | Overloading * and [] |
Fn, FnMut, FnOnce | Callable values |
Send, Sync, Sized, Unpin | Marker traits |
Error | Error values |
Future | Asynchronous values |
Most iterators are lazy. Adapters like map, filter and
zip return new iterators, and consumers like collect,
sum and fold run them.
let total: i32 = (1..=10)
.filter(|n| n % 2 == 0)
.map(|n| n * n)
.sum(); // 220
let names: Vec<String> = vec!["a", "b"].into_iter()
.map(|s| s.to_owned())
.collect();