Is Box special with respect to the orphan rule?

⚓ Rust    📅 2026-02-27    👤 surdeus    👁️ 1      

surdeus

I seem to be able to do

pub trait My {
    fn hello(&self) -> Self;
}

impl PartialEq for Box<dyn My> {
    fn eq(&self, other: &Self) -> bool {
        todo!()
    }
}

But I can't seem to do

pub trait My {
    fn hello(&self) -> Self;
}

impl PartialEq for Rc<dyn My> {
    fn eq(&self, other: &Self) -> bool {
        todo!()
    }
}

Only traits defined in the current crate can be implemented for arbitrary types [E0117]

I know that Box is special, and I assume that's why that happens. But can I rely on that staying this way? Presumably, Rust will want to eventually treat Box non-specially, and thus, would the first example eventually stop working too?

4 posts - 3 participants

Read full topic

🏷️ Rust_feed