title: Can I do template specialisation with concrete types in rust?
body: <p>I am a beginner in rust, but used to do C++. One thing that you can do in C++ is template specialisation, e.g.</p>
  <pre><code>// A generic sort function
  template &lt;class T&gt;
  void sort(T arr[], int size)
  {
      // code to implement Quick Sort
  }
   
  // Template Specialization: A function specialized for char data type
  template &lt;&gt;
  void sort&lt;char&gt;(char arr[], int size)
  {
      // code to implement counting sort
  }
  </code></pre>
  <p>(see <a href="https://www.geeksforgeeks.org/template-specialization-c/" rel="nofollow noreferrer">blogpost</a>).</p>
  <p>In the above example Quick sort is selected as a default implementation, but counting sort is picked if we want to sort chars.</p>
  <p>I played around with Rust, but could not get this to work</p>
  <pre><code>fn test&lt;T: std::fmt::Display&gt;(arg: T) {
      println!(&quot;T implements Displai trait '{}'&quot;, arg)
  }
  
  fn test&lt;T&gt;(arg: T) {
      println!(&quot;default version called&quot;)
  }
  </code></pre>
  <p>This gives <code>the name test is defined multiple times</code>.</p>
  <p>In Rust, can I create generic functions that pick their implementation based on the type of the argument? If so, can I do this with concrete types, like in C++?</p>
  <p>The only similar thing I could find was this post, working with traits, not with concrete types (<a href="https://stackoverflow.com/questions/48135833/specialize-method-for-one-specific-generic-type">link here</a>)</p>
  <p><strong>Edit</strong></p>
  <pre><code>struct Mytype&lt;T&gt; {
      val: T,
  }
  
  impl&lt;T&gt; Mytype&lt;T&gt; {
      fn test(&amp;self) {
          println!(&quot;any type T&quot;)
      }
  }
  impl Mytype&lt;f64&gt; {
      fn test2(&amp;self) {
          println!(&quot;called with f64&quot;)
      }
  }
  
  fn main() {
      Mytype { val: &quot;&quot; }.test();
      Mytype { val: 1.0 }.test();
      Mytype { val: 1.0 }.test2();
  }
  </code></pre>
  <p>In the above example I have a specialised method based on the template type. Here <code>test2</code> is only available if you use a <code>f64</code>. HOWEVER I still need two different function names, which is not what I want.</p>