trait-concept
Rust Sealed trait pattern
A library can restrict outside implementations of a public trait by requiring an inaccessible supertrait. This is a library design pattern, not a Rust keyword. It can preserve implementation control, but also prevents downstream extension. Expected output (not executed): meter Why: Consumers can use Unit but cannot name the private Sealed trait from outside library. Common mistake: Re-exporting Sealed publicly and unintentionally allowing external implementations.
Formal Syntax & Structure
rust syntaxrust
pub trait Public: private::Sealed { /* items */ }Working Example
Example Snippetrust
mod library { mod private { pub trait Sealed {} } pub trait Unit: private::Sealed { fn name(&self) -> &str; } pub struct Meter; impl private::Sealed for Meter {} impl Unit for Meter { fn name(&self) -> &str { "meter" } }}use library::Unit;fn main() { println!("{}", library::Meter.name()); }