title: How can I approximate method overloading?
body: <p>I am modeling an API where method overloading would be a good fit. My naïve attempt failed:</p>
  
  <pre><code>// fn attempt_1(_x: i32) {}
  // fn attempt_1(_x: f32) {}
  // Error: duplicate definition of value `attempt_1`
  </code></pre>
  
  <p>I then added an enum and worked through to:</p>
  
  <pre><code>enum IntOrFloat {
      Int(i32),
      Float(f32),
  }
  
  fn attempt_2(_x: IntOrFloat) {}
  
  fn main() {
      let i: i32 = 1;
      let f: f32 = 3.0;
  
      // Can't pass the value directly
      // attempt_2(i);
      // attempt_2(f);
      // Error: mismatched types: expected enum `IntOrFloat`
  
      attempt_2(IntOrFloat::Int(i));
      attempt_2(IntOrFloat::Float(f));
      // Ugly that the caller has to explicitly wrap the parameter
  }
  </code></pre>
  
  <p>Doing some quick searches, I've <a href="https://mail.mozilla.org/pipermail/rust-dev/2012-April/001649.html" rel="noreferrer">found</a> <a href="https://github.com/rust-lang/rust/issues/1838" rel="noreferrer">some</a> <a href="https://github.com/rust-lang/rust/wiki/Rust-for-CXX-programmers#function-overloading" rel="noreferrer">references</a> that talk about overloading, and all of them seem to end in "we aren't going to allow this, but give traits a try". So I tried:</p>
  
  <pre><code>enum IntOrFloat {
      Int(i32),
      Float(f32),
  }
  
  trait IntOrFloatTrait {
      fn to_int_or_float(&amp;self) -&gt; IntOrFloat;
  }
  
  impl IntOrFloatTrait for i32 {
      fn to_int_or_float(&amp;self) -&gt; IntOrFloat {
          IntOrFloat::Int(*self)
      }
  }
  
  impl IntOrFloatTrait for f32 {
      fn to_int_or_float(&amp;self) -&gt; IntOrFloat {
          IntOrFloat::Float(*self)
      }
  }
  
  fn attempt_3(_x: &amp;dyn IntOrFloatTrait) {}
  
  fn main() {
      let i: i32 = 1;
      let f: f32 = 3.0;
  
      attempt_3(&amp;i);
      attempt_3(&amp;f);
      // Better, but the caller still has to explicitly take the reference
  }
  </code></pre>
  
  <p>Is this the closest I can get to method overloading? Is there a cleaner way?</p>