title: How can I create multiple items in a Rust procedural macro?
body: <p>I am trying to create a macro that, when used like this:</p>
  
  <pre><code>foo!()
  </code></pre>
  
  <p>expands to two items, a static item and a function. Something like this:</p>
  
  <pre><code>static x: uint = 5;
  
  fn bar() -&gt; uint { x + 1 }
  </code></pre>
  
  <p>It has been pointed out to me that <code>MacResult</code>'s <code>make_items</code> method supports this, so I just need to create a type that implements it properly. I have done so with this:</p>
  
  <pre><code>struct MacItems {
      items: Vec&lt;::std::gc::Gc&lt;Item&gt;&gt;,
  }
  
  impl MacResult for MacItems {
      fn make_def(&amp;self) -&gt; Option&lt;::syntax::ext::base::MacroDef&gt; { None }
      fn make_expr(&amp;self) -&gt; Option&lt;::std::gc::Gc&lt;ast::Expr&gt;&gt; { None }
      fn make_pat(&amp;self) -&gt; Option&lt;::std::gc::Gc&lt;ast::Pat&gt;&gt; { None }
      fn make_stmt(&amp;self) -&gt; Option&lt;::std::gc::Gc&lt;ast::Stmt&gt;&gt; { None }
  
      fn make_items(&amp;self) -&gt; Option&lt;::syntax::util::small_vector::SmallVector&lt;::std::gc::Gc&lt;Item&gt;&gt;&gt; {
          Some(::syntax::util::small_vector::SmallVector::many(self.items.clone()))
      }
  }
  </code></pre>
  
  <p>I have attempted to use the <code>quote_item!</code> macro, but this macro apparently moves the <code>ExtCtxt</code>, making it not possible to use it twice in a row. Here's my code:</p>
  
  <pre><code>#[plugin_registrar]
  pub fn plugin_registrar(reg: &amp;mut Registry) {
      reg.register_macro("foo", expand);
  }
  
  fn expand(cx: &amp;mut ExtCtxt, sp: codemap::Span, _: &amp;[ast::TokenTree]) -&gt; Box&lt;MacResult&gt; {
      let mut v = vec!();
  
      v.push( quote_item!(cx, static x: uint = 5;).unwrap() );
      v.push( quote_item!(cx, fn bar() -&gt; uint { x + 1 }).unwrap() );
  
      box MacItems { items: v } as Box&lt;MacResult&gt;
  }
  </code></pre>
  
  <p>And here's the error:</p>
  
  <pre><code>test.rs:37:13: 37:57 error: use of moved value: `cx`
  test.rs:37     v.push( quote_item!(cx, fn bar() -&gt; uint { x + 1 }).unwrap() );
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  note: in expansion of quote_item!
  test.rs:37:13: 37:57 note: expansion site
  test.rs:36:13: 36:50 note: `cx` moved here because it has type `&amp;mut syntax::ext::base::ExtCtxt&lt;'_&gt;`, which is moved by default (use `ref` to override)
  test.rs:36     v.push( quote_item!(cx, static x: uint = 5;).unwrap() );
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  note: in expansion of quote_item!
  test.rs:36:13: 36:50 note: expansion site
  error: aborting due to previous error
  </code></pre>
  
  <p>How can I achieve this? I attempted to use <code>AstBuilder::item_static</code> to create the static item, but I cannot figure out which variant of <code>Ty_</code> corresponds to <code>uint</code> here. (Also this is just a toy example, my real static declaration is <code>&amp;'static str</code>, so I need to build that item instead).</p>
  
  <p>Am I doing this completely wrong? How can this be achieved?</p>