liveBook · Manning

Contract

As explained in Refactoring to Rust

An API is a defined interface through which software components expose operations and exchange data.

An API is the public-facing interface through which a crate exposes usable operations and data while hiding implementation details. In Rust, module structure can remain deeply nested internally while re-exports provide shorter, stable names at the crate root. section 5.2.2

A well-shaped Rust API:

  • Exposes the names users should call, such as tree_cover::enter or crate::DayKind. section 5.2.2
  • Hides generic helpers and internal modules when users should not call them directly. section 5.2.2
  • Uses visibility deliberately: private items hide implementation, while pub(crate) permits use within the crate without exposing it publicly. section 5.2.2
  • Acts as a contract whose behavior should remain consistent during incremental refactoring. section 1.1 section 1.3

Re-exporting a public type

A crate can define DayKind inside day_kind but publish it at the crate root with `pub use crate::day_kind::DayKind;`. Other modules then write `use crate::DayKind;`, while callers use the shorter public name without depending on the internal module path. section 5.2.2

pub use crate::day_kind::DayKind;

This listing shows the compact re-export that creates a crate-root API name. section 5.2.2

How Refactoring to Rust covers it

How deep it goes
Explains re-exports, module visibility, and crate boundaries with code, stopping short of broader API design guidance.
Examples
Uses DayKind re-exports and a forest crate that hides enter_area behind user-facing entry functions.
What it assumes
Assumes basic Rust modules and paths, while teaching public, private, and crate-visible boundaries through examples.
What you can do after
Operate/configure Rust visibility and re-exports to expose stable names while restricting internal helpers.
How it connects
Connects public API with modules, re-exports, visibility, contracts, and type systems.

Also explained in

Compare how 44 books explain contract →

Related concepts

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