Skip to content

ACP: Add NonZero::<T>::from_str_radix #548

Closed

Description

@sorairolake

Proposal

Problem statement

NonZero::<T>::from_str is already implemented and it only supports decimal, just like T::from_str. In other words, there is no direct way to convert a string which represents non-decimal such as octal and hexadecimal to NonZero<T>.

To do this, we need to combine T::from_str and NonZero::<T>::new as follows:

let n = NonZeroU8::new(u8::from_str_radix("ff", 16)?);

Also, unlike NonZero::<T>::from_str, this cannot return IntErrorKind::Zero.

Motivating examples or use cases

I think it would be useful to be able to convert a string which represents non-decimal directly to NonZero<T> like T::from_str_radix. This method also allows you to check whether the error kind is IntErrorKind::Zero or not, because it can return this error kind.

let err = NonZeroU64::from_str_radix("0", 16).unwrap_err();
assert_eq!(err.kind(), &IntErrorKind::Zero);

Unlike NonZero::<T>::from_str, NonZero::<T>::from_str_radix can also be used in constant contexts, which may be an advantage (because rust-lang/rust#67792 not yet stable and NonZero::<T>::from_str_radix is not a trait method):

const N: NonZeroU32 = if let Ok(n) = NonZeroU32::from_str_radix("644", 8) {
    n
} else {
    panic!()
};
assert_eq!(N.get(), 0o644);

Solution sketch

// `src/num/nonzero.rs`
impl NonZero<$Int> {
    pub const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
        let n = match <$Int>::from_str_radix(src, radix) {
            Ok(n) => n,
            Err(err) => return Err(err),
        };
        if let Some(n) = Self::new(n) {
            Ok(n)
        } else {
            Err(ParseIntError { kind: IntErrorKind::Zero })
        }
    }
}

Alternatives

We can do something similar by combining T::from_str and NonZero::<T>::new. We can also check whether a number is zero by checking whether NonZero::<T>::new returns None. However, existing APIs cannot return IntErrorKind::Zero.

Links and related work

https://internals.rust-lang.org/t/implement-nonzero-from-str-radix/22464

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Activity

  1. added
    api-change-proposal
    on Feb 25, 2025
  2. added
    I-libs-nominated
    on Feb 1, 2026
  3. Amanieu commented on Feb 5, 2026

    @Amanieu
    Member

    This was discussed in the @rust-lang/libs-api meeting and we are happy to accept this.

    Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created.

  4. added
    ACP-accepted
    and removed
    I-libs-nominated
    on Feb 5, 2026
  5. sorairolake commented on Feb 5, 2026

    @sorairolake
    Author

    I've opened the tracking issue rust-lang/rust#152193.

  6. added a commit that references this issue on Feb 6, 2026
  7. added 2 commits that reference this issue on Feb 6, 2026
  8. added a commit that references this issue on Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedapi-change-proposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions