Macros for all your token pasting needs
pastey is the fork of paste and is aimed to be a drop-in replacement with additional features for
paste crate
Migrating from paste crate to pastey is super simple, just change the following
in your Cargo.toml
[dependencies]
- paste = "1"
+ pastey = "*" # Or any specific version of pastey
Or even better way:
[dependencies]
- paste = "1"
+ paste = { package = "pastey", version = "*" }
Quick Start
Add pastey as your dependency in Cargo.toml
[]
# TODO: Replace with latest version available on crates.io
= "*"
This approach works with any Rust compiler 1.54+.
Pasting identifiers
Within the paste! macro, identifiers inside [<...>] are pasted together to
form a single identifier.
use paste;
paste!
More elaborate example
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.
use paste;
}
}
make_a_struct_and_getters!;
Case conversion
The pastey crate supports the following case modfiers:
| Modifier | Description |
|---|---|
$var:lower |
Lower Case |
$var:upper |
Upper Case |
$var:snake |
Snake Case |
$var:camel or $var:upper_camel |
Upper Camel Case |
$var:lower_camel |
Lower Camel Case #4 |
$var:camel_edge |
Covers Edge cases of Camel Case. #3 |
NOTE: The pastey crate is going to be a drop in replacement to paste crate,
and will not change the behaviour of existing modifier like lower, upper,
snake and camel. For modifying the behaviour new modifiers will be created,
like camel_edge
You can also use multiple of these modifers like $var:snake:upper would give you
SCREAMING_SNAKE_CASE.
Example
use paste;
paste!
The precise Unicode conversions are as defined by str::to_lowercase and
str::to_uppercase.
Replace modifier
The replace modifier allows you to perform string replacement on identifiers,
using the same semantics as str::replace. This is useful for transforming
identifiers by removing or substituting substrings.
use paste;
Raw Identifier Generation
pastey now supports raw identifiers using a special raw mode. By prefixing a
token with # inside the paste syntax, it treats that token as a raw identifier.
use paste;
define_struct_and_impl!;
define_struct_and_impl!;
Pasting documentation strings
Within the paste! macro, arguments to a #[doc ...] attribute are implicitly
concatenated together to form a coherent documentation string.
use paste;
method_new!; // expands to #[doc = "Create a new `Paste` object"]