rust - 如何在 Rust 0.13.0 中获得平方根?

标签 rust rust-obsolete

在 0.13.0-nightly 中,以下代码将无法编译:

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

我得到错误:

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

我做错了什么?

最佳答案

sqrt 方法在 std::num::Float 中trait,所以你需要使用它:

use std::num::Float;

fn main() {
    let a = (10.5f64).sqrt();
    println!("{}", a);
}

打印

3.24037

Demo

关于rust - 如何在 Rust 0.13.0 中获得平方根?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531899/

相关文章:

function - rust 封闭和fn不匹配

rust - 使用 Rust 解引用运算符 &* vs * with Self?

rust - 使用图像箱时如何获得 u8 RGB 值的向量?

rust - Rust 泛型中的常量值

rust - vector 的借用引用的生命周期与其包含的借用指针之间有什么关系?

rust - FromStr 特征不暴露生命周期的原因是什么?

rust - 我怎样才能让 Serde 在反序列化期间从 arena 分配字符串?

rust - 收集到 rust 中拥有的字符串的拥有的 vec

rust - 为什么结构中的 Box<T> 为 "explicit lifetime bound required"?

使用rust "error: moving out of immutable field"