rust - PI常数不明确

标签 rust pi 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
  1. 为什么它提示“关联类型”?我在这里看到的唯一类型是 f32 ,这是不相关的。
  2. 为什么这是模棱两可的?我明确指定了 f32 .
  3. 这是什么<f32 as Trait>::consts句法?我以前从未见过。
  4. 很明显,我该怎么做才能修复此错误并将我的变量设置为 PI?

最佳答案

要解决此问题,请添加 use std::f32 或使用 std::f32::consts::PI,以便编译器知道我们在谈论关于模块 f32 这里,不是type f32

关于rust - PI常数不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208465/

相关文章:

rust - 是否可以在 Rust 的 match 语句中包含现有变量?

rust - 使用Yew框架进行Webassembly的未发现的错误

c++ - 无法打印更多的 pi 小数

c++ - 使用clog计算复数的自然对数时,如何解决出现的歧义?

python - Rust vs python 程序性能结果问题

generics - 我如何限制 Rust 中的 T 只接受数字类型?

c# - 如何在 Pi 的 C# 程序中将小数点精确到 n 位

Java 和无限小数位?

excel - 检测到不明确的名称 - 轻松修复

c# - ICollection/ICollection<T> 歧义问题