title: How to write a custom derive macro?
body: <p>I'm trying to write my own derive mode macro in Rust, and the <a href="https://doc.rust-lang.org/reference/procedural-macros.html#derive-mode-macros" rel="noreferrer">documentation</a> on it is somewhat lacking in examples.</p>
  
  <p>I have a struct like:</p>
  
  <pre><code>#[derive(MyMacroHere)]
  struct Example {
      id: i64,
      value: Option&lt;String&gt;,
  }
  </code></pre>
  
  <p>I want my macro to generate a method à la </p>
  
  <pre><code>fn set_fields(&amp;mut self, id: i64, value: Option&lt;String&gt;) {
      // ...
  }
  </code></pre>
  
  <p>What are the basic steps to use the <code>TokenStream</code> trait to achieve something like that?</p>