103 lines
9.5 KiB
HTML
103 lines
9.5 KiB
HTML
<!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="github crates-io docs-rs"><title>paste - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.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"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="paste" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate paste</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../paste/index.html">paste</a><span class="version">1.0.15</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#pasting-identifiers" title="Pasting identifiers">Pasting identifiers</a></li><li><a href="#more-elaborate-example" title="More elaborate example">More elaborate example</a></li><li><a href="#case-conversion" title="Case conversion">Case conversion</a></li><li><a href="#pasting-documentation-strings" title="Pasting documentation strings">Pasting documentation strings</a></li></ul><h3><a href="#macros">Crate Items</a></h3><ul class="block"><li><a href="#macros" title="Macros">Macros</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>paste</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/paste/lib.rs.html#1-454">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/dtolnay/paste"><img src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" alt="github" /></a> <a href="https://crates.io/crates/paste"><img src="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust" alt="crates-io" /></a> <a href="https://docs.rs/paste"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" alt="docs-rs" /></a></p>
|
||
<br>
|
||
<p>The nightly-only <a href="https://doc.rust-lang.org/std/macro.concat_idents.html"><code>concat_idents!</code></a> macro in the Rust standard library is
|
||
notoriously underpowered in that its concatenated identifiers can only refer to
|
||
existing items, they can never be used to define something new.</p>
|
||
<p>This crate provides a flexible way to paste together identifiers in a macro,
|
||
including using pasted identifiers to define new items.</p>
|
||
<p>This approach works with any Rust compiler 1.31+.</p>
|
||
<br>
|
||
<h2 id="pasting-identifiers"><a class="doc-anchor" href="#pasting-identifiers">§</a>Pasting identifiers</h2>
|
||
<p>Within the <code>paste!</code> macro, identifiers inside <code>[<</code>…<code>>]</code> are pasted
|
||
together to form a single identifier.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>paste::paste;
|
||
|
||
<span class="macro">paste!</span> {
|
||
<span class="comment">// Defines a const called `QRST`.
|
||
</span><span class="kw">const </span>[<Q R S T>]: <span class="kw-2">&</span>str = <span class="string">"success!"</span>;
|
||
}
|
||
|
||
<span class="kw">fn </span>main() {
|
||
<span class="macro">assert_eq!</span>(
|
||
<span class="macro">paste!</span> { [<Q R S T>].len() },
|
||
<span class="number">8</span>,
|
||
);
|
||
}</code></pre></div>
|
||
<p><br><br></p>
|
||
<h2 id="more-elaborate-example"><a class="doc-anchor" href="#more-elaborate-example">§</a>More elaborate example</h2>
|
||
<p>The next example shows a macro that generates accessor methods for some
|
||
struct fields. It demonstrates how you might find it useful to bundle a
|
||
paste invocation inside of a macro_rules macro.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>paste::paste;
|
||
|
||
<span class="macro">macro_rules!</span> make_a_struct_and_getters {
|
||
(<span class="macro-nonterminal">$name</span>:ident { $(<span class="macro-nonterminal">$field</span>:ident),* }) => {
|
||
<span class="comment">// Define a struct. This expands to:
|
||
//
|
||
// pub struct S {
|
||
// a: String,
|
||
// b: String,
|
||
// c: String,
|
||
// }
|
||
</span><span class="kw">pub struct </span><span class="macro-nonterminal">$name </span>{
|
||
$(
|
||
<span class="macro-nonterminal">$field</span>: String,
|
||
)*
|
||
}
|
||
|
||
<span class="comment">// Build an impl block with getters. This expands to:
|
||
//
|
||
// impl S {
|
||
// pub fn get_a(&self) -> &str { &self.a }
|
||
// pub fn get_b(&self) -> &str { &self.b }
|
||
// pub fn get_c(&self) -> &str { &self.c }
|
||
// }
|
||
</span><span class="macro">paste!</span> {
|
||
<span class="kw">impl </span><span class="macro-nonterminal">$name </span>{
|
||
$(
|
||
<span class="kw">pub fn </span>[<get_ <span class="macro-nonterminal">$field</span>>](<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span>str {
|
||
<span class="kw-2">&</span><span class="self">self</span>.<span class="macro-nonterminal">$field
|
||
</span>}
|
||
)*
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="macro">make_a_struct_and_getters!</span>(S { a, b, c });
|
||
|
||
<span class="kw">fn </span>call_some_getters(s: <span class="kw-2">&</span>S) -> bool {
|
||
s.get_a() == s.get_b() && s.get_c().is_empty()
|
||
}</code></pre></div>
|
||
<p><br><br></p>
|
||
<h2 id="case-conversion"><a class="doc-anchor" href="#case-conversion">§</a>Case conversion</h2>
|
||
<p>Use <code>$var:lower</code> or <code>$var:upper</code> in the segment list to convert an
|
||
interpolated segment to lower- or uppercase as part of the paste. For
|
||
example, <code>[<ld_ $reg:lower _expr>]</code> would paste to <code>ld_bc_expr</code> if invoked
|
||
with $reg=<code>Bc</code>.</p>
|
||
<p>Use <code>$var:snake</code> to convert CamelCase input to snake_case.
|
||
Use <code>$var:camel</code> to convert snake_case to CamelCase.
|
||
These compose, so for example <code>$var:snake:upper</code> would give you SCREAMING_CASE.</p>
|
||
<p>The precise Unicode conversions are as defined by <a href="https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase"><code>str::to_lowercase</code></a> and
|
||
<a href="https://doc.rust-lang.org/std/primitive.str.html#method.to_uppercase"><code>str::to_uppercase</code></a>.</p>
|
||
<br>
|
||
<h2 id="pasting-documentation-strings"><a class="doc-anchor" href="#pasting-documentation-strings">§</a>Pasting documentation strings</h2>
|
||
<p>Within the <code>paste!</code> macro, arguments to a #[doc …] attribute are
|
||
implicitly concatenated together to form a coherent documentation string.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>paste::paste;
|
||
|
||
<span class="macro">macro_rules!</span> method_new {
|
||
(<span class="macro-nonterminal">$ret</span>:ident) => {
|
||
<span class="macro">paste!</span> {
|
||
<span class="attr">#[doc = <span class="string">"Create a new `" </span><span class="macro-nonterminal">$ret </span><span class="string">"` object."</span>]
|
||
</span><span class="kw">pub fn </span>new() -> <span class="macro-nonterminal">$ret </span>{ <span class="macro">todo!</span>() }
|
||
}
|
||
};
|
||
}
|
||
|
||
<span class="kw">pub struct </span>Paste {}
|
||
|
||
<span class="macro">method_new!</span>(Paste); <span class="comment">// expands to #[doc = "Create a new `Paste` object"]</span></code></pre></div></div></details><h2 id="macros" class="section-header">Macros<a href="#macros" class="anchor">§</a></h2><dl class="item-table"><dt><a class="macro" href="macro.paste.html" title="macro paste::paste">paste</a></dt></dl></section></div></main></body></html> |