What Is the Prelude in Rust?

The Rust Prelude is an automatically imported module containing common traits and types like Option and Result to reduce boilerplate.

The Prelude in Rust is a standard library module automatically imported into every Rust program, providing common traits and types like Iterator, Option, and Result without explicit imports. It allows you to use these items directly, as seen when BufReader and TcpStream rely on traits from std::io::prelude or the root std::prelude to function without verbose use statements.

// The prelude is implicitly available; no 'use std::prelude::v1;' is needed.
// Common items like Iterator, Option, and Result are always in scope.
fn main() {
    let x: Option<i32> = Some(5); // Option comes from the Prelude
    let y: Result<i32, &str> = Ok(10); // Result comes from the Prelude
}