RFC 3323 'Restrictions' is now available for testing on nightly Rust, introducing two new features: `impl_restriction` and `mut_restriction`. The `impl_restriction` feature lets library authors explicitly restrict which scopes can implement a trait (e.g., `pub impl(crate) trait Foo`), replacing the verbose sealed trait pattern with a concise, compiler-enforced alternative. The `mut_restriction` feature allows struct fields to be publicly readable but only mutable within a specified scope (e.g., `pub mut(crate) alpha: u8`), enabling invariant preservation without getter methods and working better with the borrow checker's field-level tracking. Both features produce clear compiler error messages when restrictions are violated. Struct expressions are also disallowed when any field has a mut-restriction that prevents mutation from the current scope. Developers are invited to test these features on the latest nightly and share feedback in the dedicated GitHub issue, with syntax still open for discussion.

5m read timeFrom blog.rust-lang.org
Post cover image
Table of contents
What is impl_restriction ?What is mut_restriction ?How can I help?Acknowledgements

Questions this post answers

What is the impl_restriction feature in Rust and how does it replace the sealed trait pattern?

The `impl_restriction` feature (RFC 3323) lets you write `pub impl(crate) trait Foo {}` to prevent downstream crates from implementing a trait, replacing the sealed trait pattern. The sealed trait pattern required defining a private `Sealed` supertrait in a private module; `impl_restriction` achieves the same result in one line and produces a direct compiler error pointing to the restriction site when violated. Rust library authors enforcing API boundaries track nightly feature stabilization on daily.dev.

How does mut_restriction work in Rust for making struct fields publicly readable but only internally mutable?

With `mut_restriction`, you annotate a field as `pub mut(crate) alpha: u8` to allow any code to read it while restricting mutation to the current crate. Scopes like `mut(super)` or `mut(self)` are also supported. Struct expressions are disallowed outside the permitted scope when any field carries a mut-restriction, preventing construction of unvalidated values that could violate invariants. Developers designing Rust APIs with invariant-preserving structs follow RFC progress on daily.dev.

17K Impressions1 Comment