title: Function that accepts both &Vec<T> and &Vec<&T> in Rust
body: <p>In my struct I have a <code>from</code> function that takes a shared reference to a vector of elements of type <code>T</code> and does some initialisation.</p>
  <pre><code>fn from(t: &amp;Vec&lt;T&gt;) -&gt; () {
      // ...
      for a in t {
          // ...
      }
      for a in t {
          // ...
      }
  }
  </code></pre>
  <p>I also have another function <code>from_partial</code> that first performs some filtering on <code>&amp;Vec&lt;T&gt;</code> and wants to pass this reduced list of elements to <code>from</code>. I don't want to clone so <code>from_partial</code> constructs the vector as <code>Vec&lt;&amp;T&gt;</code> instead of <code>Vec&lt;T&gt;</code>. I also don't want to duplicate the logic in <code>from</code> as all in needs to do is loop over the vector and get a <code>&amp;T</code> to store somewhere.</p>
  <p>In my concrete type example, I can assign both <code>&amp;Vec&lt;&amp;Bar&gt;</code> and <code>&amp;Vec&lt;Bar&gt;</code> to <code>from</code></p>
  <pre><code>let a: &amp;Vec&lt;&amp;Bar&gt; = &amp;test.iter().collect();
  let b: &amp;Vec&lt;Bar&gt; = test;
  Foo::from(a); // can assign &amp;Vec&lt;&amp;Bar&gt;
  Foo::from(b); // can assign &amp;Vec&lt;Bar&gt;
  </code></pre>
  <p>However when working with generic types, I am unable to do so:</p>
  <pre><code>fn from_partial(t: &amp;Vec&lt;T&gt;) -&gt; () {
      // Here a function is called that does some filtering on &amp;Vec&lt;T&gt;
      // and returns &amp;Vec&lt;&amp;T&gt;, but omitting for brevity.
      let example: &amp;Vec&lt;&amp;T&gt; = &amp;t.iter().collect();
      Self::from(example); // cannot assign &amp;Vec&lt;&amp;T&gt; to &amp;Vec&lt;T&gt;?
  }
  </code></pre>
  <p>Here's the MVCE (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=208a748cd6994c01f22edaec35a00eb0" rel="nofollow noreferrer">Playground</a>):</p>
  <pre><code>struct Foo&lt;T&gt; {
      t: T,
  }
  struct Bar {}
  
  impl&lt;T&gt; Foo&lt;T&gt; {
      fn from(t: &amp;Vec&lt;T&gt;) -&gt; () {
          let mut v = Vec::new();
          for a in t {
              // ...
              v.push(Foo { t: a })
          }
          for a in t {
              // ...
              v.get(0);
          }
      }
  
      fn from_partial(t: &amp;Vec&lt;T&gt;) -&gt; () {
          // Here a function is called that does some filtering on &amp;Vec&lt;T&gt;
          // and returns &amp;Vec&lt;&amp;T&gt;, but omitting for brevity.
          let example: &amp;Vec&lt;&amp;T&gt; = &amp;t.iter().collect();
          Self::from(example); // cannot assign &amp;Vec&lt;&amp;T&gt; to &amp;Vec&lt;T&gt;?
      }
  }
  
  fn main() {}
  
  fn test(test: &amp;Vec&lt;Bar&gt;) {
      let a: &amp;Vec&lt;&amp;Bar&gt; = &amp;test.iter().collect();
      let b: &amp;Vec&lt;Bar&gt; = test;
      Foo::from(a); // can assign &amp;Vec&lt;&amp;Bar&gt;
      Foo::from(b); // can assign &amp;Vec&lt;Bar&gt;
  }
  </code></pre>
  <p>Is there any sort of constraint I can add on <code>T</code> that would make this possible? To essentially prevent me from duplicating exactly the same logic in <code>from</code> twice.</p>