External Publication
Visit Post

Mutually exclusive traits

Rust Internals [Unofficial] May 17, 2026
Source

Negative impls are particularly relevant to auto traits, but they are equally usable for all traits. I don’t know if there are any plans to make negative impls usable to enable otherwise conflicting impls.

Another way to express mutual exclusion is to define a single trait, and discriminate the two cases using an associated constant or associated type:

#![feature(min_generic_const_args)]

use std::marker::PhantomData;

trait C {}
trait AOrB: C {
    type const IS_B: bool;
}

struct Example<T: C>{ _marker: PhantomData<T> }
impl<T: AOrB<IS_B = false>> Example<T> {
  fn test() {}
}

impl<T: AOrB<IS_B = true>> Example<T> {
  fn test() {}
}

Conceptually, this should already work (if you use an associated type instead of a const), because a single impl of AOrB can’t have two values of IS_B, but the compiler doesn’t currently recognize this type of non-conflict.

Discussion in the ATmosphere

Loading comments...