Auto Generated Documentation

This commit is contained in:
github-actions[bot]
2025-04-02 13:41:38 +00:00
parent e9a232e07d
commit 21efed14f2
18523 changed files with 340249 additions and 282417 deletions
+89 -115
View File
@@ -1,22 +1,16 @@
<!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="The whole point."><meta name="keywords" content="rust, rustlang, rust-lang, quote"><title>quote in 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="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings"></script><script src="../storage.js"></script><script src="../crates.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon.svg">
<link rel="alternate icon" type="image/png" href="../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">&#9776;</div><a href='../quote/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><p class="location"><a href="index.html">quote</a></p><div id="sidebar-vars" data-name="quote" data-ty="macro" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush.svg" width="18" height="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><button type="button" class="help-button">?</button>
<a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" height="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Macro <a href="index.html">quote</a>::<wbr><a class="macro" href="">quote</a><button id="copy-path" onclick="copy_path(this)"></button></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#478-487" title="goto source code">[src]</a></span></h1><div class="docblock type-decl"><div class="example-wrap"><pre class="rust macro">
<span class="macro">macro_rules!</span> <span class="ident">quote</span> {
() <span class="op">=</span><span class="op">&gt;</span> { ... };
($(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">tt</span>:<span class="ident">tt</span>)<span class="kw-2">*</span>) <span class="op">=</span><span class="op">&gt;</span> { ... };
}</pre></div>
</div><div class="docblock"><p>The whole point.</p>
<!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="The whole point."><title>quote in quote - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="quote" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.1 (4eb161250 2025-03-15)" data-channel="1.85.1" data-search-js="search-75f5ac3e.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc macro"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../quote/index.html">quote</a><span class="version">1.0.40</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">quote</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#interpolation" title="Interpolation">Interpolation</a></li><li><a href="#hygiene" title="Hygiene">Hygiene</a></li><li><a href="#return-type" title="Return type">Return type</a></li><li><a href="#examples" title="Examples">Examples</a><ul><li><a href="#procedural-macro" title="Procedural macro">Procedural macro</a></li><li><a href="#combining-quoted-fragments" title="Combining quoted fragments">Combining quoted fragments</a></li><li><a href="#constructing-identifiers" title="Constructing identifiers">Constructing identifiers</a></li><li><a href="#making-method-calls" title="Making method calls">Making method calls</a></li><li><a href="#interpolating-text-inside-of-doc-comments" title="Interpolating text inside of doc comments">Interpolating text inside of doc comments</a></li><li><a href="#indexing-into-a-tuple-struct" title="Indexing into a tuple struct">Indexing into a tuple struct</a></li></ul></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate quote</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">quote</a></span><h1>Macro <span class="macro">quote</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/quote/lib.rs.html#482-486">Source</a> </span></div><pre class="rust item-decl"><code>macro_rules! quote {
($($tt:tt)*) =&gt; { ... };
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The whole point.</p>
<p>Performs variable interpolation against the input and produces it as
<a href="../proc_macro2/struct.TokenStream.html" title="proc_macro2::TokenStream"><code>proc_macro2::TokenStream</code></a>.</p>
<a href="../proc_macro2/struct.TokenStream.html" title="struct proc_macro2::TokenStream"><code>proc_macro2::TokenStream</code></a>.</p>
<p>Note: for returning tokens to the compiler in a procedural macro, use
<code>.into()</code> on the result to convert to <a href="https://doc.rust-lang.org/nightly/proc_macro/struct.TokenStream.html" title="proc_macro::TokenStream"><code>proc_macro::TokenStream</code></a>.</p>
<code>.into()</code> on the result to convert to <a href="https://doc.rust-lang.org/1.85.1/proc_macro/struct.TokenStream.html" title="struct proc_macro::TokenStream"><code>proc_macro::TokenStream</code></a>.</p>
<br>
<h1 id="interpolation" class="section-header"><a href="#interpolation">Interpolation</a></h1>
<h2 id="interpolation"><a class="doc-anchor" href="#interpolation">§</a>Interpolation</h2>
<p>Variable interpolation is done with <code>#var</code> (similar to <code>$var</code> in
<code>macro_rules!</code> macros). This grabs the <code>var</code> variable that is currently in
scope and inserts it in that location in the output tokens. Any type
implementing the <a href="trait.ToTokens.html"><code>ToTokens</code></a> trait can be interpolated. This includes most
implementing the <a href="trait.ToTokens.html" title="trait quote::ToTokens"><code>ToTokens</code></a> trait can be interpolated. This includes most
Rust primitive types as well as most of the syntax tree types from the <a href="https://github.com/dtolnay/syn">Syn</a>
crate.</p>
<p>Repetition is done using <code>#(...)*</code> or <code>#(...),*</code> again similar to
@@ -28,16 +22,16 @@ for each one. The variables in an interpolation may be a <code>Vec</code>, slice
<li><code>#(#var)*</code> — no separators</li>
<li><code>#(#var),*</code> — the character before the asterisk is used as a separator</li>
<li><code>#( struct #var; )*</code> — the repetition can contain other tokens</li>
<li><code>#( #k =&gt; println!(&quot;{}&quot;, #v), )*</code> — even multiple interpolations</li>
<li><code>#( #k =&gt; println!("{}", #v), )*</code> — even multiple interpolations</li>
</ul>
<br>
<h1 id="hygiene" class="section-header"><a href="#hygiene">Hygiene</a></h1>
<h2 id="hygiene"><a class="doc-anchor" href="#hygiene">§</a>Hygiene</h2>
<p>Any interpolated tokens preserve the <code>Span</code> information provided by their
<code>ToTokens</code> implementation. Tokens that originate within the <code>quote!</code>
invocation are spanned with <a href="https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html#method.call_site"><code>Span::call_site()</code></a>.</p>
<p>A different span can be provided through the <a href="macro.quote_spanned.html"><code>quote_spanned!</code></a> macro.</p>
invocation are spanned with <a href="../proc_macro2/struct.Span.html#method.call_site" title="associated function proc_macro2::Span::call_site"><code>Span::call_site()</code></a>.</p>
<p>A different span can be provided through the <a href="macro.quote_spanned.html" title="macro quote::quote_spanned"><code>quote_spanned!</code></a> macro.</p>
<br>
<h1 id="return-type" class="section-header"><a href="#return-type">Return type</a></h1>
<h2 id="return-type"><a class="doc-anchor" href="#return-type">§</a>Return type</h2>
<p>The macro evaluates to an expression of type <code>proc_macro2::TokenStream</code>.
Meanwhile Rust procedural macros are expected to return the type
<code>proc_macro::TokenStream</code>.</p>
@@ -48,56 +42,54 @@ tests and non-macro code like main.rs and build.rs. This is why even the
procedural macro ecosystem is largely built around <code>proc_macro2</code>, because
that ensures the libraries are unit testable and accessible in non-macro
contexts.</p>
<p>There is a <a href="https://doc.rust-lang.org/std/convert/trait.From.html"><code>From</code></a>-conversion in both directions so returning the output of
<p>There is a <a href="https://doc.rust-lang.org/1.85.1/core/convert/trait.From.html" title="trait core::convert::From"><code>From</code></a>-conversion in both directions so returning the output of
<code>quote!</code> from a procedural macro usually looks like <code>tokens.into()</code> or
<code>proc_macro::TokenStream::from(tokens)</code>.</p>
<br>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1><h3 id="procedural-macro" class="section-header"><a href="#procedural-macro">Procedural macro</a></h3>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2><h4 id="procedural-macro"><a class="doc-anchor" href="#procedural-macro">§</a>Procedural macro</h4>
<p>The structure of a basic procedural macro is as follows. Refer to the <a href="https://github.com/dtolnay/syn">Syn</a>
crate for further useful guidance on using <code>quote!</code> as part of a procedural
macro.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">proc_macro</span>;
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">extern crate </span>proc_macro;
<span class="kw">use</span> <span class="ident">proc_macro::TokenStream</span>;
<span class="kw">use</span> <span class="ident">quote::quote</span>;
<span class="kw">use </span>proc_macro::TokenStream;
<span class="kw">use </span>quote::quote;
<span class="attribute">#[<span class="ident">proc_macro_derive</span>(<span class="ident">HeapSize</span>)]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">derive_heap_size</span>(<span class="ident">input</span>: <span class="ident">TokenStream</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">TokenStream</span> {
<span class="comment">// Parse the input and figure out what implementation to generate...</span>
<span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
<span class="kw">let</span> <span class="ident">expr</span> <span class="op">=</span> <span class="comment">/* ... */</span>;
<span class="attr">#[proc_macro_derive(HeapSize)]
</span><span class="kw">pub fn </span>derive_heap_size(input: TokenStream) -&gt; TokenStream {
<span class="comment">// Parse the input and figure out what implementation to generate...
</span><span class="kw">let </span>name = <span class="comment">/* ... */</span>;
<span class="kw">let </span>expr = <span class="comment">/* ... */</span>;
<span class="kw">let</span> <span class="ident">expanded</span> <span class="op">=</span> <span class="macro">quote!</span> {
<span class="comment">// The generated impl.</span>
<span class="kw">impl</span> <span class="ident">heapsize::HeapSize</span> <span class="kw">for</span> #<span class="ident">name</span> {
<span class="kw">fn</span> <span class="ident">heap_size_of_children</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">usize</span> {
#<span class="ident">expr</span>
<span class="kw">let </span>expanded = <span class="macro">quote!</span> {
<span class="comment">// The generated impl.
</span><span class="kw">impl </span>heapsize::HeapSize <span class="kw">for </span>#name {
<span class="kw">fn </span>heap_size_of_children(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; usize {
#expr
}
}
};
<span class="comment">// Hand the output tokens back to the compiler.</span>
<span class="ident">TokenStream::from</span>(<span class="ident">expanded</span>)
}</pre></div>
<span class="comment">// Hand the output tokens back to the compiler.
</span>TokenStream::from(expanded)
}</code></pre></div>
<p><br></p>
<h3 id="combining-quoted-fragments" class="section-header"><a href="#combining-quoted-fragments">Combining quoted fragments</a></h3>
<h4 id="combining-quoted-fragments"><a class="doc-anchor" href="#combining-quoted-fragments">§</a>Combining quoted fragments</h4>
<p>Usually you dont end up constructing an entire final <code>TokenStream</code> in one
piece. Different parts may come from different helper functions. The tokens
produced by <code>quote!</code> themselves implement <code>ToTokens</code> and so can be
interpolated into later <code>quote!</code> invocations to build up a final result.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">type_definition</span> <span class="op">=</span> <span class="macro">quote!</span> {...};
<span class="kw">let</span> <span class="ident">methods</span> <span class="op">=</span> <span class="macro">quote!</span> {...};
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>type_definition = <span class="macro">quote!</span> {...};
<span class="kw">let </span>methods = <span class="macro">quote!</span> {...};
<span class="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="macro">quote!</span> {
#<span class="ident">type_definition</span>
#<span class="ident">methods</span>
};</pre></div>
<span class="kw">let </span>tokens = <span class="macro">quote!</span> {
#type_definition
#methods
};</code></pre></div>
<p><br></p>
<h3 id="constructing-identifiers" class="section-header"><a href="#constructing-identifiers">Constructing identifiers</a></h3>
<h4 id="constructing-identifiers"><a class="doc-anchor" href="#constructing-identifiers">§</a>Constructing identifiers</h4>
<p>Suppose we have an identifier <code>ident</code> which came from somewhere in a macro
input and we need to modify it in some way for the macro output. Lets
consider prepending the identifier with an underscore.</p>
@@ -105,111 +97,93 @@ consider prepending the identifier with an underscore.</p>
behavior of concatenating them. The underscore and the identifier will
continue to be two separate tokens as if you had written <code>_ x</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// incorrect</span>
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="kw">_</span>#<span class="ident">ident</span> <span class="op">=</span> <span class="number">0</span>;
}</pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// incorrect
</span><span class="macro">quote!</span> {
<span class="kw">let </span><span class="kw-2">mut </span>_#ident = <span class="number">0</span>;
}</code></pre></div>
<p>The solution is to build a new identifier token with the correct value. As
this is such a common case, the <a href="macro.format_ident.html" title="format_ident!"><code>format_ident!</code></a> macro provides a
this is such a common case, the <a href="macro.format_ident.html" title="macro quote::format_ident"><code>format_ident!</code></a> macro provides a
convenient utility for doing so correctly.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">varname</span> <span class="op">=</span> <span class="macro">format_ident!</span>(<span class="string">&quot;_{}&quot;</span>, <span class="ident">ident</span>);
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>varname = <span class="macro">format_ident!</span>(<span class="string">"_{}"</span>, ident);
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> #<span class="ident">varname</span> <span class="op">=</span> <span class="number">0</span>;
}</pre></div>
<span class="kw">let </span><span class="kw-2">mut </span>#varname = <span class="number">0</span>;
}</code></pre></div>
<p>Alternatively, the APIs provided by Syn and proc-macro2 can be used to
directly build the identifier. This is roughly equivalent to the above, but
will not handle <code>ident</code> being a raw identifier.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">concatenated</span> <span class="op">=</span> <span class="macro">format!</span>(<span class="string">&quot;_{}&quot;</span>, <span class="ident">ident</span>);
<span class="kw">let</span> <span class="ident">varname</span> <span class="op">=</span> <span class="ident">syn::Ident::new</span>(<span class="kw-2">&amp;</span><span class="ident">concatenated</span>, <span class="ident">ident</span>.<span class="ident">span</span>());
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>concatenated = <span class="macro">format!</span>(<span class="string">"_{}"</span>, ident);
<span class="kw">let </span>varname = syn::Ident::new(<span class="kw-2">&amp;</span>concatenated, ident.span());
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> #<span class="ident">varname</span> <span class="op">=</span> <span class="number">0</span>;
}</pre></div>
<span class="kw">let </span><span class="kw-2">mut </span>#varname = <span class="number">0</span>;
}</code></pre></div>
<p><br></p>
<h3 id="making-method-calls" class="section-header"><a href="#making-method-calls">Making method calls</a></h3>
<h4 id="making-method-calls"><a class="doc-anchor" href="#making-method-calls">§</a>Making method calls</h4>
<p>Lets say our macro requires some type specified in the macro input to have
a constructor called <code>new</code>. We have the type in a variable called
<code>field_type</code> of type <code>syn::Type</code> and want to invoke the constructor.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="comment">// incorrect</span>
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> #<span class="ident">field_type::new</span>();
}</pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// incorrect
</span><span class="macro">quote!</span> {
<span class="kw">let </span>value = #field_type::new();
}</code></pre></div>
<p>This works only sometimes. If <code>field_type</code> is <code>String</code>, the expanded code
contains <code>String::new()</code> which is fine. But if <code>field_type</code> is something
like <code>Vec&lt;i32&gt;</code> then the expanded code is <code>Vec&lt;i32&gt;::new()</code> which is invalid
syntax. Ordinarily in handwritten Rust we would write <code>Vec::&lt;i32&gt;::new()</code>
but for macros often the following is more convenient.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="op">&lt;</span>#<span class="ident">field_type</span><span class="op">&gt;</span><span class="ident">::new</span>();
}</pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
<span class="kw">let </span>value = &lt;#field_type&gt;::new();
}</code></pre></div>
<p>This expands to <code>&lt;Vec&lt;i32&gt;&gt;::new()</code> which behaves correctly.</p>
<p>A similar pattern is appropriate for trait methods.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="macro">quote!</span> {
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="op">&lt;</span>#<span class="ident">field_type</span> <span class="kw">as</span> <span class="ident">core::default::Default</span><span class="op">&gt;</span><span class="ident">::default</span>();
}</pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
<span class="kw">let </span>value = &lt;#field_type <span class="kw">as </span>core::default::Default&gt;::default();
}</code></pre></div>
<p><br></p>
<h3 id="interpolating-text-inside-of-doc-comments" class="section-header"><a href="#interpolating-text-inside-of-doc-comments">Interpolating text inside of doc comments</a></h3>
<h4 id="interpolating-text-inside-of-doc-comments"><a class="doc-anchor" href="#interpolating-text-inside-of-doc-comments">§</a>Interpolating text inside of doc comments</h4>
<p>Neither doc comments nor string literals get interpolation behavior in
quote:</p>
<div class='information'><div class='tooltip compile_fail'></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="macro">quote!</span> {
<span class="doccomment">/// try to interpolate: #ident</span>
<span class="doccomment">///</span>
<span class="doccomment">/// ...</span>
}</pre></div>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile"></a><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
<span class="doccomment">/// try to interpolate: #ident
///
/// ...
</span>}</code></pre></div>
<div class='information'><div class='tooltip compile_fail'></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="macro">quote!</span> {
<span class="attribute">#[<span class="ident">doc</span> <span class="op">=</span> <span class="string">&quot;try to interpolate: #ident&quot;</span>]</span>
}</pre></div>
<p>Macro calls in a doc attribute are not valid syntax:</p>
<div class='information'><div class='tooltip compile_fail'></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="macro">quote!</span> {
<span class="attribute">#[<span class="ident">doc</span> <span class="op">=</span> <span class="macro">concat!</span>(<span class="string">&quot;try to interpolate: &quot;</span>, <span class="macro">stringify!</span>(#<span class="ident">ident</span>))]</span>
}</pre></div>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile"></a><pre class="rust rust-example-rendered"><code><span class="macro">quote!</span> {
<span class="attr">#[doc = <span class="string">"try to interpolate: #ident"</span>]
</span>}</code></pre></div>
<p>Instead the best way to build doc comments that involve variables is by
formatting the doc string literal outside of quote.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">msg</span> <span class="op">=</span> <span class="macro">format!</span>(...);
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>msg = <span class="macro">format!</span>(...);
<span class="macro">quote!</span> {
<span class="attribute">#[<span class="ident">doc</span> <span class="op">=</span> #<span class="ident">msg</span>]</span>
<span class="doccomment">///</span>
<span class="doccomment">/// ...</span>
}</pre></div>
<span class="attr">#[doc = #msg]
</span><span class="doccomment">///
/// ...
</span>}</code></pre></div>
<p><br></p>
<h3 id="indexing-into-a-tuple-struct" class="section-header"><a href="#indexing-into-a-tuple-struct">Indexing into a tuple struct</a></h3>
<h4 id="indexing-into-a-tuple-struct"><a class="doc-anchor" href="#indexing-into-a-tuple-struct">§</a>Indexing into a tuple struct</h4>
<p>When interpolating indices of a tuple or tuple struct, we need them not to
appears suffixed as integer literals by interpolating them as <a href="https://docs.rs/syn/1.0/syn/struct.Index.html"><code>syn::Index</code></a>
appears suffixed as integer literals by interpolating them as <a href="https://docs.rs/syn/2.0/syn/struct.Index.html"><code>syn::Index</code></a>
instead.</p>
<div class='information'><div class='tooltip compile_fail'></div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail">
<span class="kw">let</span> <span class="ident">i</span> <span class="op">=</span> <span class="number">0usize</span>..<span class="self">self</span>.<span class="ident">fields</span>.<span class="ident">len</span>();
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile"></a><pre class="rust rust-example-rendered"><code><span class="kw">let </span>i = <span class="number">0usize</span>..<span class="self">self</span>.fields.len();
<span class="comment">// expands to 0 + self.0usize.heap_size() + self.1usize.heap_size() + ...</span>
<span class="comment">// which is not valid syntax</span>
<span class="macro">quote!</span> {
<span class="number">0</span> #( <span class="op">+</span> <span class="self">self</span>.#<span class="ident">i</span>.<span class="ident">heap_size</span>() )<span class="op">*</span>
}</pre></div>
<span class="comment">// expands to 0 + self.0usize.heap_size() + self.1usize.heap_size() + ...
// which is not valid syntax
</span><span class="macro">quote!</span> {
<span class="number">0 </span>#( + <span class="self">self</span>.#i.heap_size() )*
}</code></pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">i</span> <span class="op">=</span> (<span class="number">0</span>..<span class="self">self</span>.<span class="ident">fields</span>.<span class="ident">len</span>()).<span class="ident">map</span>(<span class="ident">syn::Index::from</span>);
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>i = (<span class="number">0</span>..<span class="self">self</span>.fields.len()).map(syn::Index::from);
<span class="comment">// expands to 0 + self.0.heap_size() + self.1.heap_size() + ...</span>
<span class="macro">quote!</span> {
<span class="number">0</span> #( <span class="op">+</span> <span class="self">self</span>.#<span class="ident">i</span>.<span class="ident">heap_size</span>() )<span class="op">*</span>
}</pre></div>
</div></section><section id="search" class="content hidden"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="quote" data-search-index-js="../search-index.js" data-search-js="../search.js"></div>
<script src="../main.js"></script></body></html>
<span class="comment">// expands to 0 + self.0.heap_size() + self.1.heap_size() + ...
</span><span class="macro">quote!</span> {
<span class="number">0 </span>#( + <span class="self">self</span>.#i.heap_size() )*
}</code></pre></div>
</div></details></section></div></main></body></html>