Rust Reference ยท Standard Library Modules
The Rust prelude
Reference for the Rust prelude: which types, traits, functions and macros are in scope in every module, editions 2021 and 2024 changes, shadowing and no_std.
The prelude is a small list of names that the compiler imports into every module for you. It is why you can write Vec, String, Option, Some or Box without a use line. Each edition of Rust points to its own version of the list, std::prelude::rust_2021 for example, and the compiler inserts use std::prelude::rust_20xx::*; at the top of every module.
Anything that is not in the prelude must be imported or written with a full path. HashMap, Rc, Arc, Ordering, Duration and File are common examples. The prelude is deliberately small: a name in it can clash with names in every program, so only the most universal items qualify.
let names: Vec<String> = Vec::new(); // Vec and String come from the prelude
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
The prelude is not something you write; it is a fixed list per edition. These are the items in the standard library's 2024 prelude.
| Group | Items |
|---|---|
| Marker traits | Copy, Send, Sized, Sync, Unpin |
| Operator and drop traits | Drop, Fn, FnMut, FnOnce, and the drop function |
| Conversion traits | AsRef, AsMut, Into, From, TryFrom, TryInto (the last two since edition 2021) |
| Ownership and strings | Box, String, Vec, ToOwned, ToString, Clone |
| Comparison and defaults | PartialEq, Eq, PartialOrd, Ord, Default |
| Iteration | Iterator, IntoIterator, DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator (since 2021) |
| Option and Result | Option with Some and None, Result with Ok and Err |
| Async (2024) | Future, IntoFuture |
| Macros | the standard macros such as println!, vec!, format!, assert!, panic!, matches!, write!, and the built-in derives Debug, Clone, Hash and so on |
Edition 2021 added TryFrom, TryInto and FromIterator. Edition 2024 added Future and IntoFuture. Older crates compiled in an earlier edition keep their old prelude.
Usage notes
- Priority. A name defined or imported in a module shadows the prelude. A glob import (
use something::*) also beats the prelude, but it loses against explicit names in the same module. - Not in the prelude.
HashMap,HashSet,BTreeMap,VecDeque,Rc,Arc,Cell,RefCell,Mutex,Ordering,Duration,fmt,ioandPathall need ause. Items likeRead,WriteandBufReadmust be imported before you can call their methods. no_std. With#![no_std]the compiler usescore::preludeinstead. It lacksBox,String,Vec,ToStringandToOwned, which live inalloc.#![no_implicit_prelude]turns the prelude off for a module and its children. It is rare and used mostly by macro-heavy crates to test hygiene.- Your own prelude. Many libraries expose
mylib::preludewith the handful of names every user needs. This is only a convention; users import it withuse mylib::prelude::*;. - Macros such as
println!are not found through the module prelude in the same way. They come from the standard library'smacro_useprelude, and are available everywhere. - Primitive types (
i32,bool,str,char) are always in scope too, but they are built into the language rather than listed in the prelude. - Common mistake. A trait is in scope only if it is imported. If a method such as
write_allis "not found", importstd::io::Write, even though the type itself needs no import.
Related
Vec<T>,String,Option<T>,Result<T, E>,Box<T>FromandInto,use,mod- Lessons: Modules, Crates and dependencies