title: Cannot infer type for generic type parameter in struct
body: <p>I'm currently trying to implement base64 encoding/decoding in rust as part of a larger project and needed a data structure for mapping the base64 character set to ASCII and vice versa. I found a barebones implementation of a bidirectional <code>HashMap</code> in <a href="https://stackoverflow.com/questions/33613305/how-can-i-model-a-bidirectional-map-without-annoying-the-borrow-checker">this StackOverflow answer</a>, but since I need the base64 map to be a global variable in order for both encode/decode functions to access it, I can't just add the character mappings one by one. So I added another function to the <code>BidiMap</code> implementation that does it in one batch (other function implementations are the same as in the link above):</p>
  <pre><code>impl&lt;A, B&gt; BidiMap&lt;A, B&gt;
  where
    A: Eq + Hash,
    B: Eq + Hash,
  {
  
  // ...
  
    pub fn from_array_with_u8_indices(values: &amp;[B]) -&gt; BidiMap&lt;u8, B&gt;
      where B: Copy,
    {
      if values.len() &gt; u8::MAX as usize + 1 {
        panic!(&quot;This function should only be called with an array whose indices fit into an u8.&quot;);
      }
      let mut map: BidiMap&lt;u8, B&gt; = BidiMap::new();
  
      for i in 0..values.len() {
        map.insert(i as u8, values[i]);
      }
  
      map
    }
  
  // ...
  
  }
  </code></pre>
  <p>I then call this function in another file, base64.rs:</p>
  <pre><code>use crate::datastructures::bidi_map::BidiMap;
  
  const base64_values: [u8; 65] = [
    0x41, 0x42, 0x43, ... , 0x3D,
  ];
  const base64_map: BidiMap&lt;u8, u8&gt; = BidiMap::from_array_with_u8_indices(&amp;base64_values);
  </code></pre>
  <p>But when I try to compile this code, I get the error</p>
  <pre class="lang-none prettyprint-override"><code>error[E0282]: type annotations needed
    --&gt; src/encodings/base64.rs:10:37
     |
  10 | const base64_map: BidiMap&lt;u8, u8&gt; = BidiMap::from_array_with_u8_indices(&amp;base64_values);
     |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `A`
  
  </code></pre>
  <p>Even though I specified the types for <code>A</code> and <code>B</code> with <code>BidiMap&lt;u8, u8&gt;</code>. I am pretty new to programming in rust and with generics in general, so it could be that I'm missing something really obvious, but I can't figure out what the solution to this problem is. I've already tried to change the function signature and implementation to return <code>Self</code> instead of <code>BidiMap&lt;u8, B&gt;</code>, but then it complained about not being able to cast the array indices to a generic type <code>A</code> when calling <code>map.insert()</code>.</p>