title: How to work around the lack of abstract classes in rust?
body: <p>Let's say I have common logic that depends intimately on data members as well as a piece of abstract logic. How can I write this in rust types without rewriting the same code for each implementation?</p>
  <p>Here's a toy example of what I might write in scala. Note that the abstract class has concrete logic that depends on both the data member <code>name</code> and abstract logic <code>formatDate()</code>.</p>
  <pre class="lang-scala prettyprint-override"><code>abstract class Greeting(name: String) {
    def greet(): Unit = {
      println(s&quot;Hello $name\nToday is ${formatDate()}.&quot;)
    }
  
    def formatDate(): String
  }
  
  class UsaGreeting(name: String) extends Greeting {
    override def formatDate(): String = {
      // somehow get year, month, day
      s&quot;$month/$day/$year&quot;
    }
  }
  
  class UkGreeting(name: String) extends Greeting {
    override def formatDate(): String = {
      // somehow get year, month, day
      s&quot;$day/$month/$year&quot;
    }
  }
  </code></pre>
  <p>This is just a toy example, but my real life constraints are:</p>
  <ul>
  <li>I have several data members - not just one (<code>name</code>).</li>
  <li>Every subclass has the same complex methods that depends on both those data members and abstract functions specific to the subclass.</li>
  <li>For good API design, it's important the implementing <code>struct</code>s continue to hold all those data members and complex methods.</li>
  </ul>
  <p>Here are some somewhat unsatisfactory ideas I had that could make this work in rust:</p>
  <ul>
  <li>I could require a <code>get_name()</code> method on the trait that every implementation would need. But this seems unnecessarily verbose and might also cause a performance hit if the getter doesn't get inlined.</li>
  <li>I could avoid using a rust trait altogether, instead making a struct with an additional data member that implements the missing abstract logic. But this makes the abstract logic unavailable at compile time, and would definitely cause a performance hit.</li>
  <li>I could again avoid using a rust trait altogether, instead making a struct with a generic whose associated functions complete the abstract logic. So far this is my best idea, but it feels wrong to use generics to fill in missing logic.</li>
  </ul>
  <p>I'm not fully happy with these ideas, so is there a better way in rust to mix abstract logic with concrete logic that depends on data members?</p>