-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use NonZero<T>
instead of NonZeroT
#13019
Comments
@rustbot claim |
I'm struggling to see the advantage here.
Can you provide a use case for where this lint would have helped? |
@y21 I think it's useful when you need multiple types. use core::num::{NonZeroI32, NonZeroU32, NonZeroU8};
let x = NonZeroU8::new(42).unwrap();
let x = NonZeroI32::new(42).unwrap();
let x = NonZeroU32::new(42).unwrap(); Could be written as: use core::num::NonZero;
let x = NonZero::<u8>::new(42).unwrap();
let x = NonZero::<i32>::new(42).unwrap();
let x = NonZero::<u32>::new(42).unwrap(); |
An alternative version is: #![allow(unused_variables)]
#![feature(const_option)]
use core::num::NonZero;
fn main() {
let x1 = const { NonZero::new(42_u8).unwrap() };
let x2 = const { NonZero::new(42_i32).unwrap() };
let x = const { NonZero::new(42_u32).unwrap() };
} (By the way, sometimes I'd like a unwrap-less constructor for NonZero). |
What it does
NonZero<T>
stabilized with Rust 1.79.0. This allowsNonZeroI32
to be written asNonZero<i32>
.Advantage
NonZero<c_int>
).Drawbacks
MSRV is 1.79.0.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: