I need to parse a string. I don’t want to pull regexes or grammar generator or some similar big dependency, because the format is relatively simple; just a series of easily recognizable fields.
Normally in C++, and C before it, I would just use iterators. But there iterators are just pointers into the string and ranges are just pairs of pointers.
So I tried to use iterators in Rust as well. But they are too restricted. They can basically only be used for one-at-a-time processing, because there seems to be no way to get the slice that remains to be read by iterator, slice up to the iterator and similar operations that are often needed to pass the parsed bits to functions.
Obviously it can be done with indices. But I hate indices. Even in plain C I always preferred iterating with pointers over indices, because pointers can be directly accessed without having to pull the start around everywhere and because they provide at least some type-checking. Indices are just numbers, much easier to mix up.
So what should I be using? Do I really have to use indices?
A slice is internally a pair of pointers and so is an iterator. And two pointers can be converted to offset of the second from the first. They should be convertible to each other. So is it at least possible, given a string (slice)
s and a subslice
x to calculate slice
a that is the part of
x and a slice
b that is the part of
x? Without resorting to
unsafe; there is nothing unsafe on the operations (or at least more unsafe than indexing).