std-method

Unsigned power-of-two helpers

Choose a representable power-of-two capacity without assuming every request can fit. Zero is not a power of two. Expected stdout (not executed): true Some(128) None Common mistake: Calling next_power_of_two without considering overflow.

Formal Syntax & Structure

rust syntaxrust
x.is_power_of_two(); x.checked_next_power_of_two()

Working Example

Example Snippetrust
fn main() {    println!("{}", 64u8.is_power_of_two());    println!("{:?} {:?}", 65u8.checked_next_power_of_two(), 129u8.checked_next_power_of_two());}
Rust Reference