Check whether an integer's leftmost bits are ones in Rust
The problem is that have the feeling that there must be a much simple solution to this problem.


You're right! Integers in Rust have many useful bitwise operations (as well as other useful ones like wrapping and saturating arithmetic), most of which are exposed as methods on the type. To see the full list, look at the generated documentation for the type, e.g. u128.

Here's a simple way to write is_valid_ipv6_mask:

fn is_valid_ipv6_mask(value: u128) -&gt; bool {
    value.count_zeros() == value.trailing_zeros()
}