-
Book Overview & Buying
-
Table Of Contents
Design Patterns and Best Practices in Rust
By :
Many object-oriented languages introduce the concept of sealed or final types, which are types that cannot extend beyond a defined scope. Java has final classes, Kotlin has sealed classes, and Scala has sealed traits. The pattern addresses a real problem in API design: sometimes, allowing arbitrary external implementations of an interface creates more problems than it solves.
Rust doesn't have a built-in sealed keyword, but the module system provides a way to achieve the same effect. Following its appearance in various standard library crates and its documentation in API design discussions, the technique gained widespread recognition in the Rust community. While not unique to Rust, the pattern fits naturally with Rust's privacy model and module system.
The sealed traits pattern uses Rust's module privacy to create traits that are visible for use but restricted for implementation. This provides library authors with control over trait implementations...