title: Create flexible constructor method accepting either String or &str
body: <p>I am completely new to rust. I created a program to store information about persons to get in touch with the language:</p>
  <p><code>person.rs</code></p>
  <pre class="lang-rust prettyprint-override"><code>pub struct Person {
      firstname: String,
      lastname: String,
      pub age: u8
  }
  
  
  impl Person {
      pub fn new(firstname: String, lastname: String, age: u8) -&gt; Person {
          return Person { firstname: firstname, lastname: lastname, age: age };
      }
  
      pub fn from_str(firstname: &amp;str, lastname: &amp;str, age: u8) -&gt; Person {
          return Person::new(firstname.to_string(), lastname.to_string(), age);
      }
  
      pub fn name(&amp;self) -&gt; String {
          return self.firstname.to_owned() + &quot; &quot; + &amp;self.lastname;
      }
  }
  </code></pre>
  <p><code>main.rs</code></p>
  <pre class="lang-rust prettyprint-override"><code>mod person;
  use person::Person;
  
  
  fn main() {
      let me = Person::from_str(&quot;John&quot;, &quot;Doe&quot;, 42);
      println!(&quot;{} is {} years old.&quot;, me.name(), me.age);
  }
  </code></pre>
  <p>I understand, that there is no function overloading in rust.
  However, I have read about a concept called &quot;traits&quot;, which I have not fully understood.
  Do they behave similar to templates in C++ (I'm also not a C++ pro)?
  Is there another way, e.g. using traits, so that I can write <code>Person::new()</code> to accept either <code>String</code> or <code>&amp;str</code>?</p>
  <p>I tried this to no avail:</p>
  <p><code>person.rs</code></p>
  <pre class="lang-rust prettyprint-override"><code>pub struct Person {
      firstname: String,
      lastname: String,
      pub age: u8
  }
  
  
  impl Person {
      /*
      pub fn new(firstname: String, lastname: String, age: u8) -&gt; Person {
          return Person { firstname: firstname, lastname: lastname, age: age };
      }
  
      pub fn from_str(firstname: &amp;str, lastname: &amp;str, age: u8) -&gt; Person {
          return Person::new(firstname.to_string(), lastname.to_string(), age);
      }
      */
      
      pub fn new&lt;T&gt;(firstname: &amp;T, lastname: &amp;T, age: u8) -&gt; Person {
          return Person::new(firstname.to_string(), lastname.to_string(), age);
      }
  
      pub fn name(&amp;self) -&gt; String {
          return self.firstname.to_owned() + &quot; &quot; + &amp;self.lastname;
      }
  }
  </code></pre>
  <p><code>main.rs</code></p>
  <pre class="lang-rust prettyprint-override"><code>mod person;
  use person::Person;
  
  
  fn main() {
      let me = Person::new(&quot;John&quot;, &quot;Doe&quot;, 42);
      println!(&quot;{} is {} years old.&quot;, me.name(), me.age);
  }
  </code></pre>
  <p>Resulting in:</p>
  <pre><code>$ rustc main.rs -o main
  error[E0599]: the method `to_string` exists for reference `&amp;T`, but its trait bounds were not satisfied
    --&gt; person.rs:20:38
     |
  20 |         return Person::new(firstname.to_string(), lastname.to_string(), age);
     |                                      ^^^^^^^^^ method cannot be called on `&amp;T` due to unsatisfied trait bounds
     |
     = note: the following trait bounds were not satisfied:
             `T: std::fmt::Display`
             which is required by `T: ToString`
             `&amp;T: std::fmt::Display`
             which is required by `&amp;T: ToString`
  
  error[E0599]: the method `to_string` exists for reference `&amp;T`, but its trait bounds were not satisfied
    --&gt; person.rs:20:60
     |
  20 |         return Person::new(firstname.to_string(), lastname.to_string(), age);
     |                                                            ^^^^^^^^^ method cannot be called on `&amp;T` due to unsatisfied trait bounds
     |
     = note: the following trait bounds were not satisfied:
             `T: std::fmt::Display`
             which is required by `T: ToString`
             `&amp;T: std::fmt::Display`
             which is required by `&amp;T: ToString`
  
  error[E0277]: the size for values of type `str` cannot be known at compilation time
    --&gt; main.rs:6:14
     |
  6  |     let me = Person::new(&quot;John&quot;, &quot;Doe&quot;, 42);
     |              ^^^^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `Sized` is not implemented for `str`
  note: required by a bound in `Person::new`
    --&gt; person.rs:19:16
     |
  19 |     pub fn new&lt;T&gt;(firstname: &amp;T, lastname: &amp;T, age: u8) -&gt; Person {
     |                ^ required by this bound in `Person::new`
  help: consider relaxing the implicit `Sized` restriction
    --&gt; person.rs:19:17
     |
  19 |     pub fn new&lt;T: ?Sized&gt;(firstname: &amp;T, lastname: &amp;T, age: u8) -&gt; Person {
     |                 ++++++++
  
  error: aborting due to 3 previous errors
  
  Some errors have detailed explanations: E0277, E0599.
  For more information about an error, try `rustc --explain E0277`.
  </code></pre>