Radar · Rust · Archive · Week 33 · Aug 10 – 16, 2026
Call for testing: Restricting trait implementability and field mutability
Worth knowingcompiler
- What changed
- The Rust team is testing new restrictions on trait implementability and field mutability.
- Production impact
- The source does not say.
- Try it
- Read the call for testing to understand the proposed restrictions and how they might affect your code.
- Source
- blog.rust-lang.org/inside-rust/2026/08/10/call-for-testing-impl-and-mut-restrictions/
Understand it, then run it
Rust lets you define *traits* that describe shared behavior. Sometimes you want a trait to be usable by everyone, but you don’t want other crates to add their own implementations. The new impl_restriction syntax lets you write pub impl(crate) trait Foo so that only code inside the same crate can implement Foo. Before this, you had to create a hidden Sealed trait and make Foo depend on it, which was longer and less obvious.
The mut_restriction feature does something similar for struct fields. If a field is declared pub mut(crate) alpha: u8, any crate can read alpha, but only code inside the crate that defined the struct can change it. Without this, you would normally provide a getter method and hide the field, or use unsafe tricks.
Both features aim to give clearer, compiler‑checked boundaries for who can extend or mutate your API.
Run it now
// This example demonstrates the current way to restrict trait
// implementations and field mutability before the nightly features
// are available. It uses the sealed‑trait pattern for impl
// restriction and a private field with a public getter for mut
// restriction.
mod private {
// The Sealed trait is private and cannot be named by downstream crates.
pub trait Sealed {}
}
pub trait Foo: private::Sealed {
fn method(&self);
}
// Only the types we choose can implement Sealed, so only they can
// implement Foo. This mimics the future `impl(crate)` syntax.
impl private::Sealed for usize {}
impl private::Sealed for i32 {}
impl Foo for usize {
fn method(&self) {
println!("usize implementation");
}
}
impl Foo for i32 {
fn method(&self) {
println!("i32 implementation");
}
}
// For field mutability, expose a read‑only field and provide a
// private setter. Downstream crates can read but cannot modify.
pub struct Bar {
pub alpha: u8, // public read
beta: u8, // private write
}
impl Bar {
// Public constructor
pub fn new(alpha: u8, beta: u8) -> Self {
Self { alpha, beta }
}
// Public getter for beta
pub fn beta(&self) -> u8 {
self.beta
}
// Private method to mutate beta
fn set_beta(&mut self, v: u8) {
self.beta = v;
}
}
fn main() {
// Trait implementation restriction works
let x: usize = 5;
x.method(); // prints "usize implementation"
// Field mutability restriction works
let mut b = Bar::new(10, 20);
println!("alpha: {}, beta: {}", b.alpha, b.beta()); // read allowed
// b.beta = 30; // ❌ cannot assign to private field
b.set_beta(30); // ✅ only within the crate
}
What it printed when we ran it on Rust 1.98.1 (edition 2024)
usize implementation alpha: 10, beta: 20
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed