Longest prefix of Vec<u8> that is an i32/f32

I know about str::from_utr8 and also string.parse::<T>()

Question:

I have a Vec<u8> where a prefix of it may or may not be a i32/f32. Besides (1) rolling my own parser or (2) brute forcing the length of the vec, is there a way to extract the longest prefix of the Vec that is a i32/f32 ?

I think the atoi crate will do what you want:

Or, if you know which bytes are allowed to appear immediately after the end of the number, you can use position to find the index of the first such byte, and then parse everything before it using bytes[..position].parse::<i32>().

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.