Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exponent - power

  • pow

  • We can use the pow method to get the exponent of a number, but Rust needs to know the exact type of that number.

  • It can be set explicitly or implicitly as in the case of the function returning an i16 number.

  • We have to be careful as pow can overflow.

fn main() {
    // let x  = 3;
    // println!("{}", x.pow(2));
    // can't call method `pow` on ambiguous numeric type `{integer}`
    // you must specify a type for this binding, like `i32`

    let x: i32 = 3;
    println!("{}", x.pow(2));

    let y: i8 = 10;
    println!("{}", y.pow(2));

    let lucky_number = get_lucky_number();
    println!("{}", lucky_number.pow(2));

    // let y: i8 = 16;
    // println!("{}", y.pow(2));
    // panic!: attempt to multiply with overflow
}

fn get_lucky_number() -> i16 {
    23
}