std-method

Integer ilog2, ilog10 and checked_ilog

Integer logarithms give the floor of the logarithm. The checked variant rejects zero and a base below two. Expected stdout (not executed): 4 2 Some(4) None Common mistake: Mistaking ilog10 for digit count; positive digit count is ilog10 + 1.

Formal Syntax & Structure

rust syntaxrust
n.ilog2(); n.ilog10(); n.checked_ilog(base)

Working Example

Example Snippetrust
fn main() {    println!("{} {}", 31u32.ilog2(), 999u32.ilog10());    println!("{:?} {:?}", 81u32.checked_ilog(3), 0u32.checked_ilog(2));}
Rust Reference