Type annotation for associated constants

I have a situation where I need to club related constants with a type
and the type is parametrised over V. Now when I use the constant
outside the type’s impl, like the example given below:

struct Value<V> {
    value: V,

impl<V> Value<V> {
    const VALUE_FLAG: u64 = 0x1000000000000000;

fn main() {
    let x = 0x20 | Value::VALUE_FLAG;
    println!("{}", x);

I get following error:

error[E0282]: type annotations needed
  --> orphan_param.rs:17:20
17 |     let x = 0x20 | Value::VALUE_FLAG;
   |                    ^^^^^^^^^^^^^^^^^ cannot infer type for `V`

Any suggestion on how to get rid of this error ?

You could do

impl Value<()> { const VALUE_FLAG: u64 = 0x0; }

but that may come back to bite you after a while

Thanks for the suggestion. Looks like following idea fixes it:

fn main() {
    let x = 0x20 | Value::<()>::VALUE_FLAG;
    println!("{}", x);


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.