Ben Chuanlong Du's Blog

It is never too late to learn.

Sealed Trait in Rust

In [ ]:
:timing
:sccache 1

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Rust does not have a keyword or macro tag to define a sealed trait. However, a sealed trait can be implemented as below in Rust.

pub trait Yo: private::Sealed {}

impl Yo for u8 {}

mod private {
    pub trait Sealed {}
    impl Sealed for u8 {}
}

For more discussions, please refer to Sealed traits protect against downstream implementations (C-SEALED) .

In [ ]:

Comments