PI常数不明确
PI constant is ambiguous
考虑以下代码:
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
- 为什么它会抱怨 "associated type"?我在这里看到的唯一类型是
f32
,它没有关联。
- 为什么这么暧昧?我明确指定了
f32
.
<f32 as Trait>::consts
语法是什么?没见过
- 很明显,我该怎么做才能修复此错误并将我的变量设置为 PI?
要解决这个问题,添加use std::f32
或使用std::f32::consts::PI
,这样编译器就知道我们在谈论模块 f32
这里,不是 type f32
。
What is this <f32 as Trait>::consts
syntax? I've never seen it before.
目前称为 "universal function call syntax" http://doc.rust-lang.org/stable/book/ufcs.html,但我们正在谈论不再这样称呼它,因为这不是函数...它更像是一种扩展的、明确的形式.
考虑以下代码:
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
- 为什么它会抱怨 "associated type"?我在这里看到的唯一类型是
f32
,它没有关联。 - 为什么这么暧昧?我明确指定了
f32
. <f32 as Trait>::consts
语法是什么?没见过- 很明显,我该怎么做才能修复此错误并将我的变量设置为 PI?
要解决这个问题,添加use std::f32
或使用std::f32::consts::PI
,这样编译器就知道我们在谈论模块 f32
这里,不是 type f32
。
What is this
<f32 as Trait>::consts
syntax? I've never seen it before.
目前称为 "universal function call syntax" http://doc.rust-lang.org/stable/book/ufcs.html,但我们正在谈论不再这样称呼它,因为这不是函数...它更像是一种扩展的、明确的形式.