Hi fellow Rustaceans!
I have this snippet in a hot loop inferring the data type of a string.
// leading zeroes indicate a string
// unless its followed by a period
// (e.g. 07094 is a string, 0.70 is a float )
let mut chars = string.chars();
if chars.next() == Some('0')
&& chars.next().unwrap_or_default() != '.'
&& !chars.next().unwrap_or_default().is_ascii_digit()
{
return TString;
}
if string.parse::<i64>().is_ok() {
return TInteger;
}
if string.parse::<f64>().is_ok() {
return TFloat;
}
Is the check for leading zeroes at the beginning the most efficient way to do this?
I was thinking the compiler will short-circuit on the first condition so it shouldn't be very expensive and doing the chars.next is more efficient than a regex.
Thanks in advance!