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