How to Create Public APIs with Proper Encapsulation in Rust

Expose public APIs in Rust by using the pub keyword on modules and items while keeping internal logic private.

Create public APIs in Rust by marking modules and items with pub while keeping internal helpers private, then re-export only the necessary symbols at the crate root.

// src/lib.rs
pub mod kinds;
pub mod utils;

pub use kinds::PrimaryColor;
pub use utils::mix;

// src/kinds.rs
pub enum PrimaryColor {
    Red,
    Yellow,
    Blue,
}

// src/kinds.rs (continued)
pub enum SecondaryColor {
    Orange,
    Green,
    Purple,
}

// src/utils.rs
use crate::kinds::*;

pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {
    SecondaryColor::Orange
}

This structure exposes PrimaryColor and mix to users while hiding internal details like SecondaryColor unless explicitly re-exported.