index.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `quote` crate."><meta name="keywords" content="rust, rustlang, rust-lang, quote"><title>quote - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><p class='location'>Crate quote</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'quote', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>quote</a></span><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../src/quote/lib.rs.html#1-252' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Quasi-quoting without a Syntex dependency, intended for use with <a href="https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md">Macros
  2. 1.1</a>.</p>
  3. <pre><code class="language-toml">[dependencies]
  4. quote = &quot;0.3&quot;
  5. </code></pre>
  6. <div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><pre class="rust rust-example-rendered ignore">
  7. <span class="attribute">#[<span class="ident">macro_use</span>]</span>
  8. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">quote</span>;</pre>
  9. <p>Interpolation is done with <code>#var</code>:</p>
  10. <pre><code class="language-text">let tokens = quote! {
  11. struct SerializeWith #generics #where_clause {
  12. value: &amp;'a #field_ty,
  13. phantom: ::std::marker::PhantomData&lt;#item_ty&gt;,
  14. }
  15. impl #generics serde::Serialize for SerializeWith #generics #where_clause {
  16. fn serialize&lt;S&gt;(&amp;self, s: &amp;mut S) -&gt; Result&lt;(), S::Error&gt;
  17. where S: serde::Serializer
  18. {
  19. #path(self.value, s)
  20. }
  21. }
  22. SerializeWith {
  23. value: #value,
  24. phantom: ::std::marker::PhantomData::&lt;#item_ty&gt;,
  25. }
  26. };
  27. </code></pre>
  28. <p>Repetition is done using <code>#(...)*</code> or <code>#(...),*</code> very similar to <code>macro_rules!</code>:</p>
  29. <ul>
  30. <li><code>#(#var)*</code> - no separators</li>
  31. <li><code>#(#var),*</code> - the character before the asterisk is used as a separator</li>
  32. <li><code>#( struct #var; )*</code> - the repetition can contain other things</li>
  33. <li><code>#( #k =&gt; println!(&quot;{}&quot;, #v), )*</code> - even multiple interpolations</li>
  34. </ul>
  35. <p>The return type of <code>quote!</code> is <code>quote::Tokens</code>. Tokens can be interpolated into
  36. other quotes:</p>
  37. <pre><code class="language-text">let t = quote! { /* ... */ };
  38. return quote! { /* ... */ #t /* ... */ };
  39. </code></pre>
  40. <p>Call <code>to_string()</code> or <code>as_str()</code> on a Tokens to get a <code>String</code> or <code>&amp;str</code> of Rust
  41. code.</p>
  42. <p>The <code>quote!</code> macro relies on deep recursion so some large invocations may fail
  43. with &quot;recursion limit reached&quot; when you compile. If it fails, bump up the
  44. recursion limit by adding <code>#![recursion_limit = &quot;128&quot;]</code> to your crate. An even
  45. higher limit may be necessary for especially large invocations.</p>
  46. </div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
  47. <table>
  48. <tr class=' module-item'>
  49. <td><a class="macro" href="macro.quote.html"
  50. title='macro quote::quote'>quote</a></td>
  51. <td class='docblock-short'>
  52. <p>The whole point.</p>
  53. </td>
  54. </tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  55. <table>
  56. <tr class=' module-item'>
  57. <td><a class="struct" href="struct.ByteStr.html"
  58. title='struct quote::ByteStr'>ByteStr</a></td>
  59. <td class='docblock-short'>
  60. <p>Wrap a <code>&amp;str</code> so it interpolates as a byte-string: <code>b&quot;abc&quot;</code>.</p>
  61. </td>
  62. </tr>
  63. <tr class=' module-item'>
  64. <td><a class="struct" href="struct.Hex.html"
  65. title='struct quote::Hex'>Hex</a></td>
  66. <td class='docblock-short'>
  67. <p>Wrap an integer so it interpolates as a hexadecimal.</p>
  68. </td>
  69. </tr>
  70. <tr class=' module-item'>
  71. <td><a class="struct" href="struct.Ident.html"
  72. title='struct quote::Ident'>Ident</a></td>
  73. <td class='docblock-short'>
  74. <p>An identifier that should be interpolated without quotes.</p>
  75. </td>
  76. </tr>
  77. <tr class=' module-item'>
  78. <td><a class="struct" href="struct.Tokens.html"
  79. title='struct quote::Tokens'>Tokens</a></td>
  80. <td class='docblock-short'>
  81. <p>Tokens produced by a <code>quote!(...)</code> invocation.</p>
  82. </td>
  83. </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
  84. <table>
  85. <tr class=' module-item'>
  86. <td><a class="trait" href="trait.ToTokens.html"
  87. title='trait quote::ToTokens'>ToTokens</a></td>
  88. <td class='docblock-short'>
  89. <p>Types that can be interpolated inside a <code>quote!(...)</code> invocation.</p>
  90. </td>
  91. </tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "quote";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>