Skip to content

Commit 0f52e31

Browse files
authored
Rollup merge of rust-lang#151274 - count_zeros_note, r=jhpratt
Include a link to `count_ones` in the docs for `uN::count_zeros` [docs only] I did not update `iN` because `count_zeros` is reasonable for *negative* numbers. Rendered: <img width="1188" height="524" alt="image" src="https://github.com/user-attachments/assets/80ebf53c-3cde-4230-b436-d2148b07d4c8" /> <img width="1186" height="517" alt="image" src="https://github.com/user-attachments/assets/544d2ff4-ee35-44f7-8d50-65fc5f0ff3c9" />
2 parents 845316c + b49539e commit 0f52e31

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

library/core/src/num/uint_macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ macro_rules! uint_impl {
9393
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
9494
/// assert_eq!(max.count_zeros(), 0);
9595
/// ```
96+
///
97+
/// This is heavily dependent on the width of the type, and thus
98+
/// might give surprising results depending on type inference:
99+
/// ```
100+
/// # fn foo(_: u8) {}
101+
/// # fn bar(_: u16) {}
102+
/// let lucky = 7;
103+
/// foo(lucky);
104+
/// assert_eq!(lucky.count_zeros(), 5);
105+
/// assert_eq!(lucky.count_ones(), 3);
106+
///
107+
/// let lucky = 7;
108+
/// bar(lucky);
109+
/// assert_eq!(lucky.count_zeros(), 13);
110+
/// assert_eq!(lucky.count_ones(), 3);
111+
/// ```
112+
/// You might want to use [`Self::count_ones`] instead, or emphasize
113+
/// the type you're using in the call rather than method syntax:
114+
/// ```
115+
/// let small = 1;
116+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::count_zeros(small), ", stringify!($BITS_MINUS_ONE) ,");")]
117+
/// ```
96118
#[stable(feature = "rust1", since = "1.0.0")]
97119
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
98120
#[must_use = "this returns the result of the operation, \

0 commit comments

Comments
 (0)