title: How do you implement specific types on generic traits in rust?
body: <p>I initially assumed you could do this, because the documentation (<a href="http://doc.rust-lang.org/rust.html#implementations">http://doc.rust-lang.org/rust.html#implementations</a>) suggests you can:</p>
  
  <pre><code>trait Bar&lt;T&gt; {
    fn ex(&amp;self) -&gt; T;
  }
  
  struct Foo {
    y:f64
  }
  
  impl Bar&lt;int&gt; for Foo {
    fn ex(&amp;self) -&gt; int {
      return self.y.floor() as int;
    }
  }
  
  impl Bar&lt;uint&gt; for Foo {
    fn ex(&amp;self) -&gt; uint {
      if (self.y &lt; 0.0) {
        return 0u;
      }
      return self.y.floor() as uint;
    }
  }
  </code></pre>
  
  <p>...but that doesn't seem to work. I get errors like:</p>
  
  <pre><code>error: multiple applicable methods in scope
  error: expected Bar&lt;uint&gt;, but found Bar&lt;int&gt; (expected uint but found int)
  error: expected Bar&lt;int&gt;, but found Bar&lt;uint&gt; (expected int but found uint)
  </code></pre>
  
  <p>So I figured perhaps Foo has to be generic for this to work, so each specific Foo has it's own Bar implementation on it:</p>
  
  <pre><code>trait Bar&lt;T&gt; {
    fn ex(&amp;self) -&gt; T;
  }
  
  struct Foo&lt;T&gt; {
    y:f64
  }
  
  impl&lt;T&gt; Foo&lt;T&gt; {
    fn new&lt;U&gt;(value:f64) -&gt; Foo&lt;U&gt; {
      return Foo { y: value } as Foo&lt;U&gt;;
    }
  }
  
  impl Bar&lt;int&gt; for Foo&lt;int&gt; {
    fn ex(&amp;self) -&gt; int {
      return self.y.floor() as int;
    }
  }
  
  impl Bar&lt;uint&gt; for Foo&lt;uint&gt; {
    fn ex(&amp;self) -&gt; uint {
      if (self.y &lt; 0.0) {
        return 0u;
      }
      return self.y.floor() as uint;
    }
  }
  
  fn main() {
    let z = Foo::new::&lt;int&gt;(100.5);
    let q = Foo::new::&lt;uint&gt;(101.5);
    let i:int = z.ex();
    let j:uint = q.ex();
  }
  </code></pre>
  
  <p>...but my constructor seems to not work:</p>
  
  <pre><code>x.rs:11:12: 11:38 error: non-scalar cast: `Foo&lt;&lt;generic #1&gt;&gt;` as `Foo&lt;U&gt;`
  x.rs:11     return Foo { y: value } as Foo&lt;U&gt;;
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
  error: aborting due to previous error
  </code></pre>
  
  <p>Edit: I also tried:</p>
  
  <pre><code>impl&lt;T&gt; Foo&lt;T&gt; {
    fn new&lt;U&gt;(value:f64) -&gt; Foo&lt;U&gt; {
      let rtn:Foo&lt;U&gt; = Foo { y: value };
      return rtn;
    }
  }
  </code></pre>
  
  <p>Which solve the casting error, but results in:</p>
  
  <pre><code>x.rs:32:11: 32:26 error: cannot determine a type for this expression: unconstrained type
  x.rs:32   let z = Foo::new::&lt;int&gt;(100.5);
                    ^~~~~~~~~~~~~~~
  </code></pre>
  
  <p>O_o I have no idea what that means.</p>
  
  <p>How do you do this?</p>