A crate prelude¶
Level: 201 · working knowledge
One line: use bevy::prelude::*; is not a language feature — it is an ordinary module a crate author filled with pub use lines so one glob brings in the names you almost always need — and std's own prelude is the same idea, applied by the compiler to every module.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- Writing one:
pub mod prelude { pub use crate::{Thing, OtherThing}; pub use crate::ext::MyExt as _; }— including theas _trick that brings a trait's methods into scope without its name - How glob imports resolve against explicit ones: an explicit
useor a local item shadows a glob name, and two globs with the same name are an error only if you use that name - std's prelude by edition: what
rust_2021added (TryFrom,TryInto,FromIterator) and what the 2024 prelude added — verify against the edition guide ↗ - Why preludes mostly hold traits: a method needs its trait in scope — see a trait must be in scope
- The cost: a reader cannot tell where a name came from, and a crate adding a name to its prelude can clash with yours
io::preludein std as a small, readable example
The trap it exists for¶
Glob-importing two preludes that both export a name — two different Results, or two Errors — and getting an ambiguity error at the first use, far from either use line. Import the second crate's items by name, or alias one.
Where this sits¶
Bringing names in with use covers globs and renames in general. A trait must be in scope is the reason preludes exist. This page is the pattern a crate author builds from both.
See also¶
- Bringing names in with
use— globs, renames and the collision a glob causes - A trait must be in scope — why preludes are mostly traits
- Extension traits — the traits a prelude most often re-exports
- Modules and visibility —
pub useas re-export - Resources and plugins — Bevy's prelude in use
- What Rust: The Practical Guide says about packages, crates and modules, run — a library whose whole public surface is four
pub uselines, and the private path thatE0603s
If you are coming from another language¶
- Python.
from package import *guided by__all__is the same mechanism with the same readability cost; Python's builtins are its std prelude. - Java.
java.lang.*is imported implicitly — the std prelude — and a wildcardimport com.x.*is the crate prelude, minus the trait-scope reason for it. - C++.
using namespace std;is the cautionary version: a glob into a namespace large enough to collide with anything.
Po polsku¶
Preludium skrzyni (use bevy::prelude::*;) to nie cecha języka, tylko zwykły moduł wypełniony liniami pub use, żeby jeden import z gwiazdką przyniósł najczęściej potrzebne nazwy. Preludium biblioteki standardowej działa tak samo, tylko dokłada je kompilator do każdego modułu — i jego zawartość zależy od edycji.
Szukaj po polsku: preludium skrzyni · import z gwiazdką · rust prelude module pattern · rust 2024 prelude changes