title: How to implement a trait for any sequence of elements?
body: <p>I am trying to implement a trait for any sequence of elements, so that it will work for vectors, arrays and slices. So far I've tried several approaches, but I can't compile any of them :(</p>
  
  <p>I have this trait, and a function that uses it, and a basic data type implementing the trait:</p>
  
  <pre><code>trait Hitable {
      fn hit(&amp;self, val: f64) -&gt; bool;
  }
  
  fn check_hit&lt;T: Hitable&gt;(world: &amp;T) -&gt; bool {
      world.hit(1.0)
  }
  
  struct Obj(f64);
  
  impl Hitable for Obj {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.0 &gt; val
      }
  }
  </code></pre>
  
  <p>I'd like to be able to implement that trait for sequence of <code>Obj</code>'s.
  It works fine if I just restrict it to vectors:</p>
  
  <pre><code>impl&lt;T&gt; Hitable for Vec&lt;T&gt;
  where
      T: Hitable,
  {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.iter().any(|h| h.hit(val))
      }
  }
  
  fn main() {
      let v = vec![Obj(2.0), Obj(3.0)];
      println!("{}", check_hit(&amp;v));
  }
  </code></pre>
  
  <p>But I want to make it more generic so that it works for arrays and slices; how can I do that?</p>
  
  <p>I tried the following four attempts:</p>
  
  <h3>Attempt #1: for iterator on Hitables.</h3>
  
  <pre><code>// It's not clear how to call it:
  //    vec.iter().hit(...) does not compile
  //    vec.into_iter().hit(...) does not compile
  //
  impl&lt;T, U&gt; Hitable for T
  where
      T: Iterator&lt;Item = U&gt;,
      U: Hitable,
  {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.any(|h| h.hit(val))
      }
  }
  </code></pre>
  
  <h3>Attempt #2: for something which can be turned into iterator.</h3>
  
  <pre><code>// Does not compile as well:
  //
  //         self.into_iter().any(|h| h.hit(val))
  //         ^^^^ cannot move out of borrowed content
  //
  impl&lt;T, U&gt; Hitable for T
  where
      T: IntoIterator&lt;Item = U&gt;,
      U: Hitable,
  {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.into_iter().any(|h| h.hit(val))
      }
  }
  </code></pre>
  
  <h3>Attempt #3: for slices.</h3>
  
  <pre><code>// This usage doesn't compile:
  //     let v = vec![Obj(2.0), Obj(3.0)];
  //     println!("{}", check_hit(&amp;v));
  //
  // It says that Hitable is not implemented for vectors.
  // When I convert vector to slice, i.e. &amp;v[..], complains about
  // unknown size in compilation time.
  impl&lt;T&gt; Hitable for [T]
  where
      T: Hitable,
  {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.iter().any(|h| h.hit(val))
      }
  }
  </code></pre>
  
  <h3>Attempt #4: for Iterator + Clone</h3>
  
  <pre><code>//     let v = vec![Obj(2.0), Obj(3.0)];
  //     println!("{}", check_hit(&amp;v.iter()));
  //
  // does not compile:
  //     println!("{}", check_hit(&amp;v.iter()));
  //                    ^^^^^^^^^ `&amp;Obj` is not an iterator
  //
  impl&lt;T, U&gt; Hitable for T
  where
      T: Iterator&lt;Item = U&gt; + Clone,
      U: Hitable,
  {
      fn hit(&amp;self, val: f64) -&gt; bool {
          self.clone().any(|h| h.hit(val))
      }
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=85849423ff9cc95c3896a7d754215a1d" rel="nofollow noreferrer">Playground link</a></p>