<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="githubcrates-iodocs-rs"><title>proc_macro2 - Rust</title><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><linkrel="preload"as="font"type="font/woff2"crossoriginhref="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><linkrel="stylesheet"href="../static.files/normalize-76eba96aa4d2e634.css"><linkrel="stylesheet"href="../static.files/rustdoc-5bc39a1768837dd0.css"><metaname="rustdoc-vars"data-root-path="../"data-static-root-path="../static.files/"data-current-crate="proc_macro2"data-themes=""data-resource-suffix=""data-rustdoc-version="1.77.2 (25ef9e3d8 2024-04-09)"data-channel="1.77.2"data-search-js="search-dd67cee4cfa65049.js"data-settings-js="settings-4313503d2e1961c2.js"><scriptsrc="../static.files/storage-4c98445ec4002617.js"></script><scriptdefersrc="../crates.js"></script><scriptdefersrc="../static.files/main-48f368f3872407c8.js"></script><noscript><linkrel="stylesheet"href="../static.files/noscript-04d5337699b92874.css"></noscript><linkrel="alternate icon"type="image/png"href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><linkrel="alternate icon"type="image/png"href="../static.files/favicon-32x32-422f7d1d52889060.png"><linkrel="icon"type="image/svg+xml"href="../static.files/favicon-2c020d218678b618.svg"></head><bodyclass="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="mobile-topbar"><buttonclass="sidebar-menu-toggle"title="show sidebar"></button></nav><navclass="sidebar"><divclass="sidebar-crate"><h2><ahref="../proc_macro2/index.html">proc_macro2</a><spanclass="version">1.0.79</span></h2></div><divclass="sidebar-elems"><ulclass="block">
<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-1346">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>