Visibility & Privacy
Items are private by default. pub exposes across crates; restricted forms limit scope.
Busca en todas las páginas de la documentación
Items are private by default. pub exposes across crates; restricted forms limit scope.
pub struct Api {
pub version: u32,
secret: String,
}
impl Api {
pub fn new(secret: String) -> Self {
Self { version: 1, secret }
}
}When to reach for this: Encapsulating invariants - fields private, constructors controlled.
mod internal {
pub(crate) fn helper() {}
pub(super) fn parent_only() {}
}
pub fn public_api() {
internal::helper();
}What this demonstrates:
pub(crate) visible entire cratepub(super) parent module onlypub(in path) custom path visibility (Rust 2018+). Struct private fields with public type ok if in same crate patterns differ for consumers.
pub(crate) default internal APIs.#[cfg(test)] in same module.#[macro_export] is public crate root.| Alternative | Use When | Don't Use When |
|---|---|---|
pub(crate) | Internal API | Needs external use |
#[doc(hidden)] | Hide docs but public | True privacy needed |
sealed trait module | Limit impls | Open trait |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 19 jul 2026