1Macros for all your token pasting needs
2=======================================
3
4[![Build Status](https://api.travis-ci.org/dtolnay/paste.svg?branch=master)](https://travis-ci.org/dtolnay/paste)
5[![Latest Version](https://img.shields.io/crates/v/paste.svg)](https://crates.io/crates/paste)
6[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/paste)
7
8The nightly-only [`concat_idents!`] macro in the Rust standard library is
9notoriously underpowered in that its concatenated identifiers can only refer to
10existing items, they can never be used to define something new.
11
12[`concat_idents!`]: https://doc.rust-lang.org/std/macro.concat_idents.html
13
14This crate provides a flexible way to paste together identifiers in a macro,
15including using pasted identifiers to define new items.
16
17```toml
18[dependencies]
19paste = "0.1"
20```
21
22This approach works with any stable or nightly Rust compiler 1.30+.
23
24<br>
25
26## Pasting identifiers
27
28There are two entry points, `paste::expr!` for macros in expression position and
29`paste::item!` for macros in item position.
30
31Within either one, identifiers inside `[<`...`>]` are pasted together to form a
32single identifier.
33
34```rust
35// Macro in item position: at module scope or inside of an impl block.
36paste::item! {
37    // Defines a const called `QRST`.
38    const [<Q R S T>]: &str = "success!";
39}
40
41fn main() {
42    // Macro in expression position: inside a function body.
43    assert_eq!(
44        paste::expr! { [<Q R S T>].len() },
45        8,
46    );
47}
48```
49
50<br>
51
52## More elaborate examples
53
54This program demonstrates how you may want to bundle a paste invocation inside
55of a more convenient user-facing macro of your own. Here the `routes!(A, B)`
56macro expands to a vector containing `ROUTE_A` and `ROUTE_B`.
57
58```rust
59const ROUTE_A: &str = "/a";
60const ROUTE_B: &str = "/b";
61
62macro_rules! routes {
63    ($($route:ident),*) => {{
64        paste::expr! {
65            vec![$( [<ROUTE_ $route>] ),*]
66        }
67    }}
68}
69
70fn main() {
71    let routes = routes!(A, B);
72    assert_eq!(routes, vec!["/a", "/b"]);
73}
74```
75
76The next example shows a macro that generates accessor methods for some struct
77fields.
78
79```rust
80macro_rules! make_a_struct_and_getters {
81    ($name:ident { $($field:ident),* }) => {
82        // Define a struct. This expands to:
83        //
84        //     pub struct S {
85        //         a: String,
86        //         b: String,
87        //         c: String,
88        //     }
89        pub struct $name {
90            $(
91                $field: String,
92            )*
93        }
94
95        // Build an impl block with getters. This expands to:
96        //
97        //     impl S {
98        //         pub fn get_a(&self) -> &str { &self.a }
99        //         pub fn get_b(&self) -> &str { &self.b }
100        //         pub fn get_c(&self) -> &str { &self.c }
101        //     }
102        paste::item! {
103            impl $name {
104                $(
105                    pub fn [<get_ $field>](&self) -> &str {
106                        &self.$field
107                    }
108                )*
109            }
110        }
111    }
112}
113
114make_a_struct_and_getters!(S { a, b, c });
115
116fn call_some_getters(s: &S) -> bool {
117    s.get_a() == s.get_b() && s.get_c().is_empty()
118}
119```
120
121<br>
122
123## Case conversion
124
125Use `$var:lower` or `$var:upper` in the segment list to convert an interpolated
126segment to lower- or uppercase as part of the paste. For example, `[<ld_
127$reg:lower _expr>]` would paste to `ld_bc_expr` if invoked with $reg=`Bc`.
128
129Use `$var:snake` to convert CamelCase input to snake\_case.
130Use `$var:camel` to convert snake\_case to CamelCase.
131These compose, so for example `$var:snake:upper` would give you SCREAMING\_CASE.
132
133The precise Unicode conversions are as defined by [`str::to_lowercase`] and
134[`str::to_uppercase`].
135
136[`str::to_lowercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
137[`str::to_uppercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_uppercase
138
139<br>
140
141#### License
142
143<sup>
144Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
1452.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
146</sup>
147
148<br>
149
150<sub>
151Unless you explicitly state otherwise, any contribution intentionally submitted
152for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
153be dual licensed as above, without any additional terms or conditions.
154</sub>
155