title: How do I provide an implementation of a generic struct in Rust?
body: <p>I have a struct <code>MyStruct</code> that takes a generic parameter <code>T: SomeTrait</code>, and I want to implement a <code>new</code> method for <code>MyStruct</code>. This works:</p>
  
  <pre><code>/// Constraint for the type parameter `T` in MyStruct
  pub trait SomeTrait: Clone {}
  
  /// The struct that I want to construct with `new`
  pub struct MyStruct&lt;T: SomeTrait&gt; {
      value: T,
  }
  
  fn new&lt;T: SomeTrait&gt;(t: T) -&gt; MyStruct&lt;T&gt; {
      MyStruct { value: t }
  }
  
  fn main() {}
  </code></pre>
  
  <p>I wanted to put the <code>new</code> function inside an <code>impl</code> block like this:</p>
  
  <pre><code>impl MyStruct {
      fn new&lt;T: SomeTrait&gt;(t: T) -&gt; MyStruct&lt;T&gt; {
          MyStruct { value: t }
      }
  }
  </code></pre>
  
  <p>But that fails to compile with:</p>
  
  <pre><code>error[E0107]: wrong number of type arguments: expected 1, found 0
   --&gt; src/main.rs:9:6
    |
  9 | impl MyStruct {
    |      ^^^^^^^^ expected 1 type argument
  </code></pre>
  
  <p>If I try to put it like this:</p>
  
  <pre><code>impl MyStruct&lt;T&gt; {
      fn new(t: T) -&gt; MyStruct&lt;T&gt; {
          MyStruct { value: t }
      }
  }
  </code></pre>
  
  <p>The error changes to:</p>
  
  <pre><code>error[E0412]: cannot find type `T` in this scope
   --&gt; src/main.rs:9:15
    |
  9 | impl MyStruct&lt;T&gt; {
    |               ^ not found in this scope
  </code></pre>
  
  <p>How do I provide an implementation of a generic struct? Where do I put the generic parameters and their constraints?</p>