As you could see from RFC 2500 the <[T]>::contains method is already incompatible with the Needle API
. The only intersections are <str>::contains and <OsStr>::contains which should be possible with
impl<P: for<'a> Needle<&'a str>> Contains<P> for str {
fn contains(&self, needle: P) -> bool {
core::needle::contains(self, needle)
}
}
This totally does not work for overloading the in operator. If you write x in y, would x be moved, would y be moved?
trait Contains<Lhs> {
fn contains(&self, lhs: &Lhs) -> bool; // ?
fn contains(self, lhs: Lhs) -> bool; // ??
fn contains(&self, lhs: Lhs) -> bool; // ????
fn contains(self, lhs: &Lhs) -> bool; // ????????
}
If x would be passed by reference then it cannot support needles that are !Copy. OTOH if x would be passed by value it may be surprising that a value would be consumed by a boolean operator (a == b is not consuming, a + b is consuming). However it is very clear that y.contains(x) will move the x.
If we introduce x in y using for a in b as the model, this means both x and y should be consumed, and you’ll need to write &x in &y most of the time. Is this really still ergonomic compared with y.contains(&x)?
Because of this I’m opposed to adding in as an operator in Rust.