Help for Syntax for constraining the associated type

Hi all,
Gettting this error below and tried multiple things, but not sure of the syntax to restrict the associated type

360 | fn get_value(&mut self, index: usize) -> OptionSelf::Item {
| ------------------ expected std::option::Option<f64> because of return type
361 | self.indicator.get_value(index)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found associated type
|
= note: expected enum std::option::Option<f64>
found enum std::option::Option<<T as traits::Indicator>::Item>
= help: consider constraining the associated type <T as traits::Indicator>::Item to f64
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

Hello @hftautobot, and welcome :slightly_smiling_face:

  • A few "meta" remarks about the forum and formatting

    You can write code or text snippets using triple backquotes (``` ) followed by the "syntax highlighting flavor" of your choice on the same line, and then putting the snippet on a new line afterwards, and finally ending the snippet with yet another set of triple backquotes:

    ```<language>
    ... snippet ...
    ... here ...
    ```
    

    Two main examples:

    • Use text as the <language> for no syntax highlighting (e.g., compiler error messages):

      ```text
      error[E0308]: mismatched types
        --> src/lib.rs:20:9
         |
      18 |     fn get_value (&mut self, index: usize) -> Option<Self::Item>
         |                                               ------------------ expected `Option<f64>` because of return type
      19 |     {
      20 |         self.indicator.get_value(index)
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found associated type
         |
         = note: expected enum `Option<f64>`
                    found enum `Option<<T as Indicator>::Item>`
         = help: consider constraining the associated type `<T as Indicator>::Item` to `f64`
         = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
      ```
      
      preview
      error[E0308]: mismatched types
        --> src/lib.rs:20:9
         |
      18 |     fn get_value (&mut self, index: usize) -> Option<Self::Item>
         |                                               ------------------ expected `Option<f64>` because of return type
      19 |     {
      20 |         self.indicator.get_value(index)
         |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found associated type
         |
         = note: expected enum `Option<f64>`
                    found enum `Option<<T as Indicator>::Item>`
         = help: consider constraining the associated type `<T as Indicator>::Item` to `f64`
         = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
      
    • Use rust (or nothing at all) as the <language> for Rust syntax highlighting:

      ```rust
      fn main ()
      {}
      ```
      
      preview
      fn main ()
      {}
      

    Finally, feel free to try to write minimal reproducible code snippets in The Playground; you will have an option on the top right menus to share a link to it.

    All of this will make it far easier for users of this forum to understand the issue you are facing, and thus to help you more quickly and with more accurate suggestions / solutions :slight_smile:


Since we are lacking some context, I have inferred that your code may look like:

mod traits {
    pub
    trait Indicator {
        type Item;
        
        fn get_value (&mut self, index: usize) -> Option<Self::Item>
        ;
    }
}
use traits::Indicator;

struct Wrapper<T> {
    indicator: T,
}

impl<T : Indicator> Indicator for Wrapper<T> {
    type Item = f64;

    fn get_value (&mut self, index: usize) -> Option<Self::Item>
    {
        self.indicator.get_value(index)
    }
}

Indeed, the above code yields an error message very similar to yours:

error[E0308]: mismatched types
  --> src/lib.rs:21:9
   |
19 |     fn get_value (&mut self, index: usize) -> Option<Self::Item>
   |                                               ------------------ expected `Option<f64>` because of return type
20 |     {
21 |         self.indicator.get_value(index)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found associated type
   |
   = note: expected enum `Option<f64>`
              found enum `Option<<T as Indicator>::Item>`
   = help: consider constraining the associated type `<T as Indicator>::Item` to `f64`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
  • (The differences between the error messages may be due to different version of Rust being involved).

In that case, the issue is that the Wrapper has been specified to have an Item type of f64, even though its inner Indicator has no constraits on the return type.

In that case, depending on your needs, there are two ways to solve the compilation error.

  • Either you loosen the requirement that Wrapper yield f64s, and instead, let it return the same item type as its wrappeed .indicator: T:

      impl<T : Indicator> Indicator for Wrapper<T> {
    -     type Item = f64;
    +     type Item = T::Item;
    
          fn get_value (&mut self, index: usize) -> Option<Self::Item>
          {
              self.indicator.get_value(index)
          }
      }
    
    • Playground

    • Meta-formum aside: this is using the diff "language" for syntax highlighting.

  • Or you require the wrapped .indicator: T yield f64s too:

    - impl<T : Indicator> Indicator for Wrapper<T> {
    + impl<T : Indicator<Item = f64>> Indicator for Wrapper<T> {
          type Item = f64;
    
          fn get_value (&mut self, index: usize) -> Option<Self::Item>
          {
              self.indicator.get_value(index)
          }
      }
    
    • Note that the above can also be written as:

      impl<T> Indicator for Wrapper<T>
      where
          T : Indicator<Item = f64>,
      {
          ...
      }
      
    • Playground

2 Likes

Hey @Yandros, thank you so much for the reply and sorry for the lack of context. Amazed at how perfectly you inferred the code and have nailed it to the T. Sorry was my first post in the forum and was not aware of the rules. Thanks so much for your help and God bless you.

Also some Rust Rant :slight_smile: - One thing though I feel is that compiler could have pointed me to the syntax I should follow in the code rather than a bit of a cryptic message. Also, I feel, the compiler does suggest the issues, but when you make the changes, you see some other issues crop up, there should be a mode for the compiler to run multiple passes, assuming the changes it is suggesting and foresee.

1 Like

Personally rustc is more helpful than any other compiler I've used... though it's true it took a little while to internalize some error patterns. Anyway, as good as I find them, the error messages are constantly being improved, so perhaps someone will see this and make the associated type equality bounds suggestion a machine-applicable error (i.e. with the explicit syntax demonstrated). Or you could file a feature request asking for such.

2 Likes