Raise a number to a power in Rust


We need to explicitely define the type of the base. We can do it by the type annotation : u128 or using the type suffix _u32.

Then we use the pow function.


fn main() {
    let base: u128 = 3;
    let number = base.pow(17);
    println!("{}", number);  // 129140163

    let number = 2_u32.pow(10);
    println!("{}", number);  // 1024


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo