<main><divclass="width-limiter"><navclass="sub"><formclass="search-form"><span></span><divid="sidebar-button"tabindex="-1"><ahref="../proc_macro2/all.html"title="show sidebar"></a></div><inputclass="search-input"name="search"aria-label="Run search in the documentation"autocomplete="off"spellcheck="false"placeholder="Click or press ‘S’ to search, ‘?’ for more options…"type="search"><divid="help-button"tabindex="-1"><ahref="../help.html"title="help">?</a></div><divid="settings-menu"tabindex="-1"><ahref="../settings.html"title="settings"><imgwidth="22"height="22"alt="Change settings"src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><sectionid="main-content"class="content"><divclass="main-heading"><h1>Crate <aclass="mod"href="#">proc_macro2</a><buttonid="copy-path"title="Copy item path to clipboard"><imgsrc="../static.files/clipboard-7571035ce49a181d.svg"width="19"height="18"alt="Copy item path"></button></h1><spanclass="out-of-band"><aclass="src"href="../src/proc_macro2/lib.rs.html#1-1357">source</a> · <buttonid="toggle-all-docs"title="collapse all docs">[<span>−</span>]</button></span></div><detailsclass="toggle top-doc"open><summaryclass="hideme"><span>Expand description</span></summary><divclass="docblock"><p><ahref="https://github.com/dtolnay/proc-macro2"><imgsrc="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github"alt="github"/></a><ahref="https://crates.io/crates/proc-macro2"><imgsrc="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust"alt="crates-io"/></a><ahref="index.html"title="mod proc_macro2"><imgsrc="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs"alt="docs-rs"/></a></p>
<p>A wrapper around the procedural macro API of the compiler’s <ahref="https://doc.rust-lang.org/proc_macro/"><code>proc_macro</code></a>
crate. This library serves two purposes:</p>
<ul>
<li>
<p><strong>Bring proc-macro-like functionality to other contexts like build.rs and
main.rs.</strong> Types from <code>proc_macro</code> are entirely specific to procedural
macros and cannot ever exist in code outside of a procedural macro.
Meanwhile <code>proc_macro2</code> types may exist anywhere including non-macro code.
By developing foundational libraries like <ahref="https://github.com/dtolnay/syn">syn</a> and <ahref="https://github.com/dtolnay/quote">quote</a> against
<code>proc_macro2</code> rather than <code>proc_macro</code>, the procedural macro ecosystem
becomes easily applicable to many other use cases and we avoid
reimplementing non-macro equivalents of those libraries.</p>
</li>
<li>
<p><strong>Make procedural macros unit testable.</strong> As a consequence of being
specific to procedural macros, nothing that uses <code>proc_macro</code> can be
executed from a unit test. In order for helper libraries or components of
a macro to be testable in isolation, they must be implemented using
<p>If parsing with <ahref="https://github.com/dtolnay/syn">Syn</a>, you’ll use <ahref="https://docs.rs/syn/2.0/syn/macro.parse_macro_input.html"><code>parse_macro_input!</code></a> instead to
propagate parse errors correctly back to the compiler when parsing fails.</p>
</div></details><h2id="modules"class="section-header">Modules<ahref="#modules"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="mod"href="extra/index.html"title="mod proc_macro2::extra">extra</a></div><divclass="desc docblock-short">Items which do not have a correspondence to any API in the proc_macro crate,
but are necessary to include in proc-macro2.</div></li><li><divclass="item-name"><aclass="mod"href="token_stream/index.html"title="mod proc_macro2::token_stream">token_stream</a></div><divclass="desc docblock-short">Public implementation details for the <code>TokenStream</code> type, such as iterators.</div></li></ul><h2id="structs"class="section-header">Structs<ahref="#structs"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="struct"href="struct.Group.html"title="struct proc_macro2::Group">Group</a></div><divclass="desc docblock-short">A delimited token stream.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Ident.html"title="struct proc_macro2::Ident">Ident</a></div><divclass="desc docblock-short">A word of Rust code, which may be a keyword or legal variable name.</div></li><li><divclass="item-name"><aclass="struct"href="struct.LexError.html"title="struct proc_macro2::LexError">LexError</a></div><divclass="desc docblock-short">Error returned from <code>TokenStream::from_str</code>.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Literal.html"title="struct proc_macro2::Literal">Literal</a></div><divclass="desc docblock-short">A literal string (<code>"hello"</code>), byte string (<code>b"hello"</code>), character (<code>'a'</code>),
a suffix (<code>1</code>, <code>1u8</code>, <code>2.3</code>, <code>2.3f32</code>).</div></li><li><divclass="item-name"><aclass="struct"href="struct.Punct.html"title="struct proc_macro2::Punct">Punct</a></div><divclass="desc docblock-short">A <code>Punct</code> is a single punctuation character like <code>+</code>, <code>-</code> or <code>#</code>.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Span.html"title="struct proc_macro2::Span">Span</a></div><divclass="desc docblock-short">A region of source code, along with macro expansion information.</div></li><li><divclass="item-name"><aclass="struct"href="struct.TokenStream.html"title="struct proc_macro2::TokenStream">TokenStream</a></div><divclass="desc docblock-short">An abstract stream of tokens, or more concretely a sequence of token trees.</div></li></ul><h2id="enums"class="section-header">Enums<ahref="#enums"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="enum"href="enum.Delimiter.html"title="enum proc_macro2::Delimiter">Delimiter</a></div><divclass="desc docblock-short">Describes how a sequence of token trees is delimited.</div></li><li><divclass="item-name"><aclass="enum"href="enum.Spacing.html"title="enum proc_macro2::Spacing">Spacing</a></div><divclass="desc docblock-short">Whether a <code>Punct</code> is followed immediately by another <code>Punct</code> or followed by
another token or whitespace.</div></li><li><divclass="item-name"><aclass="enum"href="enum.TokenTree.html"title="enum proc_macro2::TokenTree">TokenTree</a></div><divclass="desc docblock-short">A single token or a delimited sequence of token trees (e.g. <code>[1, (), ..]</code>).</div></li></ul></section></div></main></body></html>