rust - Unresolved 导入 : there is no `atan` in `std::num`

标签 rust

我正在构建 Rust 教程中的示例。当我尝试编译这个例子时:

use std::float;
use std::num::atan;
fn angle(vector: (float, float)) -> float {
    let pi = float::consts::pi;
    match vector {
      (0f, y) if y < 0f => 1.5 * pi,
      (0f, y) => 0.5 * pi,
      (x, y) => atan(y / x)
    }
}

我得到了名义上的错误。我正在使用 rust build Test.rs 进行编译。为什么编译器找不到 std::num::atan

最佳答案

函数 atan 不是 std::num 的成员,因为它被定义为 impl 的一部分。但是,以下将起作用:

use std::float;

fn angle(vector: (float, float)) -> float {
    let pi = float::consts::pi;
    match vector {
      (0f, y) if y < 0f => 1.5 * pi,
      (0f, y) => 0.5 * pi,
      (x, y) => (y / x).atan()
    }
}

这是因为 atanfloat 实现的 Trigonometric 的成员。

我认为,做出此决定的原因是 Rust 中没有重载,因此为了将函数名称应用于多个具体类型,它必须是 Trait 的一部分。在这种情况下,Trigonometric 是一个数字特征,它允许方法 sincostanintfloatf64

关于rust - Unresolved 导入 : there is no `atan` in `std::num` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17935693/

相关文章:

rust - 如何修改/部分删除 BTreeMap 中的范围?

c - 在 C 程序中嵌入 Rust 任务?

rust - Rust 生命周期说明符的语法

data-structures - 如何使用不稳定的 std::collections::BitVec?

使用 clone_from_slice() 而不是 copy_from_slice() 的性能损失?

rust - 为什么枚举需要额外的内存大小?

rust - 为什么我不能写一个和 Box::new 类型相同的函数?

rust - 对于 "big"结构,只使用引用是最惯用/最有效的吗?

rust - 为什么我的 RefCell 零成本替代方案不是实现内部可变性的标准方法?

file - 如何仅获取当前可执行文件路径的目录部分?