There’s “in interval” in Rust, so has someone proposed to use “in” to allow simpler (more ergonomic) code like this (similar to D language, and Python) to search for values and subsequences? This is D language:
void main() {
auto h = [10: 20, 30: 40]; // Associative array.
if (30 in h) {}
if (30 !in h) {}
}
This doesn’t add power to language, and adds another obvious way to do something that can be done already.
#![feature(range_contains)]
fn main() {
use std::collections::{HashSet, HashMap};
let s = HashSet::<u32>::new();
if s.contains(&1) {}
// if 1 in s {}
// if 1 !in s {}
let h = HashMap::<u32, u32>::new();
if h.contains_key(&2) {}
// if 2 in h {}
// if 2 !in h {}
let r = 1 .. 10;
if r.contains(&3) {}
// if 3 in r {}
// if 3 !in r {}
let t1 = b"Hello World";
if t1.contains(&b'o') {}
// if b'o' in t1 {}
// if b'o' !in t1 {}
let v1 = vec![10, 20, 30];
if v1.contains(&20) {}
// if 20 in v1 {}
// if 20 !in v1 {}
if t1.windows(b"llo".len()).any(|w| w == b"llo") {}
// if b"llo" in t1 {}
// if b"llo" !in t1 {}
let v2 = vec![10, 20, 30, 40, 50, 60];
if v2.windows([20, 30, 40].len()).any(|w| w == [20, 30, 40]) {}
// if [20, 30, 40] in v2 {}
// if [20, 30, 40] !in v2 {}
let t2 = "Hello World";
if t2.find('W').is_some() {}
// if 'W' in t2 {}
// if 'W' !in t2 {}
if t2.find("Wor").is_some() {}
// if "Wor" in t2 {}
// if "Wor" !in t2 {}
}