Search code examples
rustrust-obsolete

How to get square root in Rust 0.13.0?


In 0.13.0-nightly the following code won't compile:

fn main() {
    let a = (10.5f64).sqrt();
}

I get the error:

error: type `f64` does not implement any method in scope named `sqrt`

What am I doing wrong?


Solution

  • sqrt method is in the std::num::Float trait, so you need to use it:

    use std::num::Float;
    
    fn main() {
        let a = (10.5f64).sqrt();
        println!("{}", a);
    }
    

    prints

    3.24037
    

    Demo