5 ms·
Is it possible to match the end of a slice with slice patterns? Something like: fn foo(s: &[char]) { match s { ['a', 'b'] => (),
by jmhain 8y ago
Is it possible to match the end of a slice with slice patterns? Something like:
fn foo(s: &[char]) {
match s {
['a', 'b'] => (),
[.. 'b', 'c'] => (),
_ => (),
}
}
- steveklabnik 8y agoNot today, but it's coming. This kind of thing does work on nightly.
- Lev1a 8y agoI was mostly excited about slice patterns because of a pattern I wanted to be able to write: some_collection.windows(2).filter(|[a,b]|a==b).map(|[a, _] |a).collect(); Turns out the compiler rejects that with a "refutable pattern" error, because the args part of the filter/map closures do not handle the situation where the slice could be empty, which AFAIK can not occur when using windows/chunks/... Maybe there has to be some special case handling for this pattern to be valid?
- steveklabnik 8y agoHm, interesting. I think this isn't possible without integer generics.
- Lev1a 8y agoBecause the pattern in the filter closure would have to be generic over the length or am I mistaken?