22

Consider following code:

fn main() {
    let i = f32::consts::PI;
}

With following error:

$ 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. Why does it complain about an "associated type"? The only type I see here is f32, which is not associated.
  2. Why is this ambigious? I clearly specified the f32.
  3. What is this <f32 as Trait>::consts syntax? I've never seen it before.
  4. And, obviously, what can I do to fix this error and get my variable set to PI?
Kapichu
  • 3,346
  • 4
  • 19
  • 38

2 Answers2

18

To solve the issue, add use std::f32 or use std::f32::consts::PI, so that the compiler knows we're talking about the module f32 here, not the type f32.

Kapichu
  • 3,346
  • 4
  • 19
  • 38
  • 5
    It's actually bad design if the module `f32` and type `f32` conflict **by default**, though this is even forbidden in Rust! – Kapichu Jul 03 '15 at 15:49
4

What is this <f32 as Trait>::consts syntax? I've never seen it before.

This is currently called "universal function call syntax" http://doc.rust-lang.org/stable/book/ufcs.html, but we're talking about not calling it that anymore, since this isn't a function... it's more of an expanded, unambiguous form.

Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
Steve Klabnik
  • 14,521
  • 4
  • 58
  • 99