r/rust•
rsamrat

[Question] Type annotations in associated constants

I have an associated constant defined in my impl block:

impl Foo {
  const THRESHOLD: f32 = 0.8;
...
}

But when I try to use the constant in a method, like so:

(self.r as f32 / self.n as f32) > LinHash::THRESHOLD

I get this error:

error[E0283]: type annotations required: cannot resolve `_: std::cmp::Eq`
  --> src/lib.rs:55:22
   |
55 |         let t: f32 = LinHash::THRESHOLD;
   |                      ^^^^^^^^^^^^^^^^^^
   |
   = note: required by `<LinHash<K, V>>::THRESHOLD`

Can someone help me understand this error? Why doesn't Rust detect that the constant is an f32? And where does it expect me to add an annotation?

Posted Oct 28, 2017, 7:42 AM UTC

68
Loading comments…