Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upParseIntError and ParseFloatError does not allow to introspect the cause #22639
Comments
kmcallister
added
the
A-libs
label
Feb 21, 2015
This comment has been minimized.
This comment has been minimized.
|
Note that this was purposefully done to avoid adding extra |
This comment has been minimized.
This comment has been minimized.
|
Triage: no change here. @rust-lang/libs is the intention still to change this to an enum of some kind in the future? |
brson
added
the
T-libs
label
May 31, 2016
This comment has been minimized.
This comment has been minimized.
|
Any reason to postpone this further? |
This comment has been minimized.
This comment has been minimized.
|
@Seeker14491 I don't think it's postponed. I think someone just needs to propose an API? |
This comment has been minimized.
This comment has been minimized.
|
Hey, we already have an enum Lines 2769 to 2774 in fc02736 |
steveklabnik
removed
the
A-libs
label
Mar 24, 2017
This comment has been minimized.
This comment has been minimized.
gypsydave5
commented
May 18, 2017
|
I just brushed up against this issue while demoing the guessing game example from the Rust Book - seemed like an easy thing to show off matching on the error kind, but turned out not so much! |
Mark-Simulacrum
added
the
C-feature-request
label
Jul 22, 2017
This comment has been minimized.
This comment has been minimized.
frontsideair
commented
Nov 9, 2017
|
I also wanted to match |
This comment has been minimized.
This comment has been minimized.
|
@frontsideair you can just check if the string's empty before parsing, right? |
This comment has been minimized.
This comment has been minimized.
frontsideair
commented
Nov 15, 2017
|
I guess, but it was a fun exercise in pattern matching. |
This comment has been minimized.
This comment has been minimized.
|
I see that checking for empty has come up in the context of the guessing game tutorial. Could somebody make a list of use cases from real code that needs to be able to distinguish between the following other ParseIntError cases?
In the event that empty is the only one we ever need to introspect for, I would prefer to close this and instead recommend to check |
This comment has been minimized.
This comment has been minimized.
|
It can be convenient to distinguish too large/too small numbers when accepting configuration parameters that the code will later sanitize by clamping to a valid range. Specifically, they make it possible to perform saturation of the parsed value before it is passed on. Example: the valid range of offsets is -10..+10, therefore the application stores it in an Without the ability to discriminate if the number is too big/small, it is not possible to determine what is the nearest legitimate value. |
This comment has been minimized.
This comment has been minimized.
|
@ranma42 do you think that use case would be solved by rust-lang/rfcs#928? use std::num::Saturating;
fn main() {
let s = "-999";
let n = match s.parse::<Saturating<i8>>()?.0 {
std::i8::MIN ... -11 => { println!("saturating"); -10 }
11 ... std::i8::MAX => { println!("saturating"); 10 }
n => { println!("in range"); n }
};
println!("n={}", n);
} |
This comment has been minimized.
This comment has been minimized.
|
Yes, that use case would be covered if Rust had the ability to parse a |
This comment has been minimized.
This comment has been minimized.
amosbatto
commented
Apr 7, 2018
•
|
The only way I found around this problem was to do something ugly like this: match my_str.parse::<i32>() {
Ok(x) => {my_int = x;},
Err(e) => {
if e.to_string() == "invalid digit found in string" {
println!("Only positive integers are allowed!");
}
else if e.to_string() == "number too large to fit in target type" {
println!("Number is too large!");
}
else if e.to_string() == "number too small to fit in target type" {
println!("Number is too small!");
}
else if e.to_string() == "cannot parse integer from empty string" {
println!("Number is empty!");
}
}
}What is so annoying is that |
This comment has been minimized.
This comment has been minimized.
Clockwork-Muse
commented
May 9, 2018
|
I'm doing custom type parsing (something like this: My map would be something like this:
....except I can't do that at the moment without resorting to @amosbatto 's trick. It also makes testing very difficult, because I can't tell what error I got back (except by checking the string). |
ethanboxx
referenced this issue
Nov 5, 2018
Merged
Make `ParseIntError` and `IntErrorKind` fully public #55705
This comment has been minimized.
This comment has been minimized.
|
Hello everyone. My PR fixes this issue and has been merged. Use the feature flag |
nagisa commentedFeb 21, 2015
The only way to introspect reason of
.parsefailure is to compare to error messages returned by description or use the following code snippet: