Hi folks. I'm learning Rust and would appreciate some feedback on this code.
Very often when I am dissecting text, I just want to divide it into two parts: the part before some pattern, and what follows the pattern. For example, with a URL I might want to split on the first colon to get the protocol. (Of course there are already URL parsers out there, but just as an example....)
I find that repeatedly splitting a slice in two like this makes code readable, because you can use destructuring, like so:
if let Some((protocol, rest)) = url.split2(':') {
Or at least you could, if split2() existed. Nesting such if-lets seems much cleaner to me than repeatedly getting Iterators and rummaging through them.
So as an exercise I wrote some code for split2() and rsplit2(), but I'm still a bit new to Rust, so I thought I'd ask for feedback. If you cared about such things, is this the way you would do it? Are there things I've done naively in the code?
And would you use Some for this, or would you create your own enum to get rid of that extra pair of parens:
if let Parts(protocol, rest) = url.split2(':') {
Of course then you'd either have to qualify Parts or use
it. Not sure it's worth it.
Thanks for any constructive criticism.