Misleading error in str::parse for int types

I encountered this one strange message when doing some basic stuff with integer parsing.

When trying to parse "+" or "-", which are not valid numbers, you get the error message "cannot parse integer from empty string". I think it's pretty misleading because the string you are parsing is not actually empty, it just happens to not have any digits in it.

For comparison, on floats you get different error messages for parsing those two strings.

See this example on the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0431bd40eb9d5cfe721fe41127cbafb4

Also, when searching the web about this, I found that the API for int parsing errors is about to be stabilized: https://github.com/rust-lang/rust/pull/76455

If this is indeed an issue then it's best if it would be sorted out before that.

IMO the most appropriate solution is introducing a NoDigits variant to IntErrorKind. Currently this case is included under the Empty variant, which I wouldn't mind if it wasn't for the error message in the case of "+" and "-".

Edit: Alternatively, rename InvalidDigits to InvalidInt: "Invalid Int literal", and have it mean what it is today plus the "+" and "-" cases. This is also more consistent with floats. And also has the same error message for things like "+" and "++". Both are just as much as invalid so why have a difference.

Thoughts?

1 Like

I think this is exactly the kind of feedback that makes sense to give during FCP, and you should post about this in that stabilization issue, if it's something you think couldn't be resolved without breaking changes.

1 Like