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

Square root (sqrt)

  • sqrt

  • as

  • f64

  • Calling the sqrt method needs to know the underlying type.

  • It suggests i32, but in fact integers don't have sqrt implemented, only floats.

  • We can convert (cast) an integer to a floating point using the as keyword.

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

    // let x: i32 = 16;
    // println!("{}", x.sqrt());
    // // // no method named `sqrt` found for type `i32` in the current scope

    let x = 16;
    let x_square = (x as f32).sqrt();
    println!("The square of {x} is {x_square}");

    let x: f32 = 16.0;
    let x_square = x.sqrt();
    println!("The square of {x} is {x_square}");
}