1 //! Error types for conversion to integral types.
3 use crate::convert::Infallible;
4 use crate::error::Error;
7 /// The error type returned when a checked integral type conversion fails.
8 #[stable(feature = "try_from", since = "1.34.0")]
9 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
10 pub struct TryFromIntError(pub(crate) ());
12 impl TryFromIntError {
14 feature = "int_error_internals",
15 reason = "available through Error trait and this method should \
16 not be exposed publicly",
20 pub fn __description(&self) -> &str {
21 "out of range integral type conversion attempted"
25 #[stable(feature = "try_from", since = "1.34.0")]
26 impl fmt::Display for TryFromIntError {
27 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
28 self.__description().fmt(fmt)
32 #[stable(feature = "try_from", since = "1.34.0")]
33 #[rustc_const_unstable(feature = "const_convert", issue = "88674")]
34 impl const From<Infallible> for TryFromIntError {
35 fn from(x: Infallible) -> TryFromIntError {
40 #[unstable(feature = "never_type", issue = "35121")]
41 impl const From<!> for TryFromIntError {
42 fn from(never: !) -> TryFromIntError {
43 // Match rather than coerce to make sure that code like
44 // `From<Infallible> for TryFromIntError` above will keep working
45 // when `Infallible` becomes an alias to `!`.
50 /// An error which can be returned when parsing an integer.
52 /// This error is used as the error type for the `from_str_radix()` functions
53 /// on the primitive integer types, such as [`i8::from_str_radix`].
55 /// # Potential causes
57 /// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
58 /// in the string e.g., when it is obtained from the standard input.
59 /// Using the [`str::trim()`] method ensures that no whitespace remains before parsing.
64 /// if let Err(e) = i32::from_str_radix("a12", 10) {
65 /// println!("Failed conversion to i32: {e}");
68 #[derive(Debug, Clone, PartialEq, Eq)]
69 #[stable(feature = "rust1", since = "1.0.0")]
70 pub struct ParseIntError {
71 pub(super) kind: IntErrorKind,
74 /// Enum to store the various types of errors that can cause parsing an integer to fail.
80 /// if let Err(e) = i32::from_str_radix("a12", 10) {
81 /// println!("Failed conversion to i32: {:?}", e.kind());
85 #[stable(feature = "int_error_matching", since = "1.55.0")]
86 #[derive(Debug, Clone, PartialEq, Eq)]
88 pub enum IntErrorKind {
89 /// Value being parsed is empty.
91 /// This variant will be constructed when parsing an empty string.
92 #[stable(feature = "int_error_matching", since = "1.55.0")]
94 /// Contains an invalid digit in its context.
96 /// Among other causes, this variant will be constructed when parsing a string that
97 /// contains a non-ASCII char.
99 /// This variant is also constructed when a `+` or `-` is misplaced within a string
100 /// either on its own or in the middle of a number.
101 #[stable(feature = "int_error_matching", since = "1.55.0")]
103 /// Integer is too large to store in target integer type.
104 #[stable(feature = "int_error_matching", since = "1.55.0")]
106 /// Integer is too small to store in target integer type.
107 #[stable(feature = "int_error_matching", since = "1.55.0")]
111 /// This variant will be emitted when the parsing string has a value of zero, which
112 /// would be illegal for non-zero types.
113 #[stable(feature = "int_error_matching", since = "1.55.0")]
118 /// Outputs the detailed cause of parsing an integer failing.
120 #[stable(feature = "int_error_matching", since = "1.55.0")]
121 pub fn kind(&self) -> &IntErrorKind {
125 feature = "int_error_internals",
126 reason = "available through Error trait and this method should \
127 not be exposed publicly",
131 pub fn __description(&self) -> &str {
133 IntErrorKind::Empty => "cannot parse integer from empty string",
134 IntErrorKind::InvalidDigit => "invalid digit found in string",
135 IntErrorKind::PosOverflow => "number too large to fit in target type",
136 IntErrorKind::NegOverflow => "number too small to fit in target type",
137 IntErrorKind::Zero => "number would be zero for non-zero type",
142 #[stable(feature = "rust1", since = "1.0.0")]
143 impl fmt::Display for ParseIntError {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 self.__description().fmt(f)
149 #[stable(feature = "rust1", since = "1.0.0")]
150 impl Error for ParseIntError {
152 fn description(&self) -> &str {
157 #[stable(feature = "try_from", since = "1.34.0")]
158 impl Error for TryFromIntError {
160 fn description(&self) -> &str {