Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Update readme for new API
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jul 26, 2020
1 parent f731b81 commit c089c81
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,20 @@ This approach works with any stable or nightly Rust compiler 1.30+.

## Pasting identifiers

There are two entry points, `paste::expr!` for macros in expression position and
`paste::item!` for macros in item position.

Within either one, identifiers inside `[<`...`>]` are pasted together to form a
single identifier.
Within the `paste!` macro, identifiers inside `[<`...`>]` are pasted together to
form a single identifier.

```rust
// Macro in item position: at module scope or inside of an impl block.
paste::item! {
use paste::paste;

paste! {
// Defines a const called `QRST`.
const [<Q R S T>]: &str = "success!";
}

fn main() {
// Macro in expression position: inside a function body.
assert_eq!(
paste::expr! { [<Q R S T>].len() },
paste! { [<Q R S T>].len() },
8,
);
}
Expand All @@ -57,12 +54,14 @@ of a more convenient user-facing macro of your own. Here the `routes!(A, B)`
macro expands to a vector containing `ROUTE_A` and `ROUTE_B`.

```rust
use paste::paste;

const ROUTE_A: &str = "/a";
const ROUTE_B: &str = "/b";

macro_rules! routes {
($($route:ident),*) => {{
paste::expr! {
paste! {
vec![$( [<ROUTE_ $route>] ),*]
}
}}
Expand All @@ -78,6 +77,8 @@ The next example shows a macro that generates accessor methods for some struct
fields.

```rust
use paste::paste;

macro_rules! make_a_struct_and_getters {
($name:ident { $($field:ident),* }) => {
// Define a struct. This expands to:
Expand All @@ -100,7 +101,7 @@ macro_rules! make_a_struct_and_getters {
// pub fn get_b(&self) -> &str { &self.b }
// pub fn get_c(&self) -> &str { &self.c }
// }
paste::item! {
paste! {
impl $name {
$(
pub fn [<get_ $field>](&self) -> &str {
Expand Down

0 comments on commit c089c81

Please sign in to comment.