title: Is there a trait for only primitive types that I can use in a generic function?
body: <p>I am trying to write a generic function which will try to convert a string to a number type like <code>i32</code>, <code>f64</code> etc.  If the string is not convertible then it will return <code>0</code>.  I am looking for an appropriate trait bound to use in my generic function below:</p>
  
  <pre><code>use std::str::FromStr;
  
  fn get_num_from_str&lt;T: FromStr&gt;(maybe_num_str: &amp;String) -&gt; T {
      let maybe_num = T::from_str(maybe_num_str.as_str());
      if maybe_num.is_ok() {
          return maybe_num.unwrap();
      }
      0 as T
  }
  
  fn main() {
      let num_str = String::from("12");
      println!("Converted to i32: {}", get_num_from_str::&lt;i32&gt;(&amp;num_str));
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=a7bea7bd83f747c6c263b7f74ee5d333&amp;version=stable&amp;mode=debug" rel="noreferrer">Playground Link</a></p>
  
  <p>I found that Rust had a <code>Primitive</code> trait before which was removed.  Is there something else now that can be used instead?</p>
  
  <p>I have found a workaround:</p>
  
  <pre><code>use std::str::FromStr;
  
  fn get_num_from_str&lt;T: Default + FromStr&gt;(maybe_num_str: &amp;String) -&gt; T {
      let maybe_num = T::from_str(maybe_num_str.as_str());
      maybe_num.unwrap_or(Default::default())
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=2b5a8151eb60578bec3441ea12a907df&amp;version=stable&amp;mode=debug" rel="noreferrer">Playground Link</a></p>
  
  <p>This, as the trait bound suggests, should work for anything which has implementations for both <code>Default</code> and <code>FromStr</code> and I should rename the function to reflect that, but it would still be nice to know if there is any trait for the primitive number types only which I can use to make sure this function cannot be used for anything other than the number types.</p>