liveBook · Manning

Prelude

As explained in Idiomatic Rust

A prelude module is a Rust module that publicly re-exports commonly used items from a crate.

A prelude module gathers useful crate items in one place, making them easier to import and helping users avoid guessing which symbols they need. section 8.4

Build one with `pub use` statements: they re-export items so code outside the module can import them. A separate `prelude` module keeps these exports distinct from other crate imports. section 8.4

Listing 8.11 Listing for prelude.rs
pub use crate::a::InnerA;
pub use crate::b::InnerB;
pub use crate::TopLevelStruct;

This prelude re-exports items from two crate modules and the crate root; users can then import them through `mylib::prelude`. section 8.4

How Idiomatic Rust covers it

How deep it goes
Explains `pub use`, wildcard imports, and a crate `prelude` module with an implementation example.
Examples
Uses a crate with modules `a` and `b`, re-exporting their items and a top-level struct from `prelude.rs`.
What it assumes
Assumes familiarity with Rust `use` imports and develops public re-exports from that syntax.
What you can do after
Build it from scratch by adding a `prelude` module that publicly re-exports selected crate items.
How it connects
Connects Rust modules, imports, public re-exports, and wildcard imports.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Idiomatic Rust. The section links point to the parts of the book it draws on.