How do I get the number of bits in `usize` in stable rust?

As per the topic, unfortunately std::usize::BITS seems to be unstable only

How about std::mem::size_of::<usize>() * 8?

7 Likes

Another fun option:

(0usize).count_zeroes()
// or
(usize::MAX).count_ones()

Of course, whichever way you get the number, it should be stuffed into a const USIZE_BITS: uN = ...;, so it doesn't really matter exactly how you get the number.

Also it's worth noting that usize::BITS is not only unstable (in Rust 1.2.0), but removed from modern stdlib distributions.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.