External Publication
Visit Post

Mutually exclusive traits

Rust Internals [Unofficial] May 17, 2026
Source

In current Rust, there doesn’t seem to be a way to declare two traits as mutually exclusive.

For example, suppose I have two traits A and B that are semantically mutually exclusive. I also have a struct Example that takes a generic parameter T: C , where both A: C and B: C .

trait C {}
trait A: C {}
trait B: C {}
// A and B are semantically mutually exclusive.
struct Example<T: C>{ _marker: PhantomData<T> }

Writing impl<T: C> Example<T> {} is perfectly fine. Then writing impl<T: A> Example<T> { fn test() {} } also seems acceptable. However, when I try to also write impl<T: B> Example<T> { fn test() {} } , the compiler complains that the two implementations conflict.

impl<T: C> Example<T> {} // ok

impl<T: A> Example<T> {
  fn test() {}
} // still ok

impl<T: B> Example<T> {
  fn test() {}
}// currently not ok

In fact, the compiler is not wrong — there could conceivably be a weird type that implements both semantically exclusive traits, leaving the compiler uncertain how to proceed. But currently, there is no way to declare that two traits are mutually exclusive. I understand that there is currently an unstable feature called "negative trait impls," but that seems to be intended for auto traits. Perhaps there is a better way to achieve this?

Discussion in the ATmosphere

Loading comments...