我正在构建 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()
}
}
这是因为 atan
是 float
实现的 Trigonometric
的成员。
我认为,做出此决定的原因是 Rust 中没有重载,因此为了将函数名称应用于多个具体类型,它必须是 Trait 的一部分。在这种情况下,Trigonometric
是一个数字特征,它允许方法 sin
、cos
、tan
、 int
、float
、f64
等
关于rust - Unresolved 导入 : there is no `atan` in `std::num` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17935693/