Possible Vec::split improvement · Issue #49036 · rust-lang/rust (original) (raw)

Hi guys! The other day I was working on something where I had some Vec<u8> data and wanted to split on two consecutive bytes. Normally with strings I can just do:

let v: Vec<&str> = "Hello\r\nWorld!".split("\r\n").collect();

But for Vec<u8> it only accepts a closure rather than a Pattern type, so it's not very easy to split on consecutive bytes. I noticed that String::split coerces closures and strings into Patterns that it uses to do the split. Do you think it would be possible (or even a good idea) to modify Vec::split and slice::split to accept any Pattern type and implement a new searcher for any PartialEq type? That would be pretty nice. Then we could do things like:

let v: Vec = b"Hello\r\nWorldWithOtherNonUtf8Stuff".split(b"\r\n").collect();

Which is useful if you're dealing with lots of bytes that may or may not be valid UTF-8.
And just generally it makes sense to me that if String::split accepts a &str, that Vec<T>::split would accept a &[T].

Maybe I'm missing something obvious that makes this impossible since I imagine someone would've done it already if it were possible.