考虑以下代码:
fn main() {
let i = f32::consts::PI;
}
出现以下错误:
$ rustc --version
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
$ rustc -
<anon>:2:13: 2:28 error: ambiguous associated type; specify the type using the syntax `<f32 as Trait>::consts` [E0223]
<anon>:2 let i = f32::consts::PI;
^~~~~~~~~~~~~~~
error: aborting due to previous error
- 为什么它提示“关联类型”?我在这里看到的唯一类型是
f32
,这是不相关的。 - 为什么这是模棱两可的?我明确指定了
f32
. - 这是什么
<f32 as Trait>::consts
句法?我以前从未见过。 - 很明显,我该怎么做才能修复此错误并将我的变量设置为 PI?
最佳答案
要解决此问题,请添加 use std::f32
或使用 std::f32::consts::PI
,以便编译器知道我们在谈论关于模块 f32
这里,不是type f32
。
关于rust - PI常数不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208465/