&[u8] split at b'.'

I can write a function
&[u8] -> Vec<&[u8]>
that splits on the b'.' u8 value. For ecample,

b"foo.bar.cat.dog.x.y.z" -> vec![b"foo", b"bar", b"cat", b"dog", b"x, b"y", b"z"]

(sort of, not exactly, as in practice the &[u8] would be a slice of the original, whereas the above is probably generating new &[u8].

Question: is there an idiomatic way to do this via builtins (rather than creating a mut vec, keeping track of indicies, and running a for loop) ?

The split() and split_mut() methods each return an iterator of subslice references that you can collect into a Vec.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.