TGViewer
Блог* Блог* @dereference_pointer_there · 1.98K subscribers
Post #11367 1.09K
#prog #rust

Call for testing: Restricting trait implementability and field mutability

Две фичи на nightly. impl_restriction позволяет указать на определении трейта, в каких модулях можно писать его реализацию.

pub mod foo {
pub mod bar {
pub(crate) impl(super) trait Foo {}
}

// `Foo` may be implemented here.
impl bar::Foo for i8 {}
}

// Error: `Foo` cannot be implemented here.
impl foo::bar::Foo for u8 {}

mut_restriction позволяет указывать на полях, в каких модулях его можно изменять:

pub mod foo {
pub struct Bar {
pub mut(self) alpha: &'static str,
}

impl Bar {
pub fn mutate_inner(&mut self) {
// `alpha` may be mutated here.
self.alpha = "inner";
}
}
}

impl foo::Bar {
pub fn mutate_outer(&mut self) {
// Error: `alpha` cannot be mutated here.
self.alpha = "outer";
}
}


Первая фича с impl(crate) позволяет выразить паттерн sealed trait напрямую.
blog.rust-lang.org Call for testing: Restricting trait implementability and field mutability | Inside Rust Blog Want to follow along with Rust development? Curious how you might get involved? Take a look!
  • 👍 8
  • 👎 5
More from @dereference_pointer_there
  1. Sep 26, 2026Эм...
  2. Sep 26, 2026#politota #meme, видимо
  3. Sep 26, 2026photo post
  4. Sep 25, 2026а чо в смысле уже пятница
  5. Sep 25, 2026Обновляем гитлабчики 💅💅💅 CVE-2026-89078 - Double Free issue in Regular Expression Parse…
  6. Sep 25, 2026photo post
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →