Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore and count require $ident instead of just ident #104

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/decl-macros/minutiae/metavar-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ As mentioned in the [`methodical introduction`](../macros-methodical.md), Rust h
This chapter will introduce them more in-depth together with usage examples.

- [`$$`](#dollar-dollar-)
- [`${count(ident, depth)}`](#countident-depth)
- [`${count($ident, depth)}`](#countident-depth)
- [`${index(depth)}`](#indexdepth)
- [`${length(depth)}`](#lengthdepth)
- [`${ignore(ident)}`](#ignoreident)
- [`${ignore($ident)}`](#ignoreident)

## Dollar Dollar (`$$`)

Expand Down Expand Up @@ -58,25 +58,25 @@ bar!();
[^tt-$]: Before `$$` occurs, users must resort to a tricky and not so well-known hack to declare nested macros with repetitions
[via using `$tt` like this](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9ce18fc79ce17c77d20e74f3c46ee13c).

## `count(ident, depth)`
## `count($ident, depth)`

The `count` metavariable expression expands to the repetition count of the metavariable `$ident` up to the given repetition depth.

- The `ident` argument must be a declared metavariable in the scope of the rule.
- The `$ident` argument must be a declared metavariable in the scope of the rule.
- The `depth` argument must be an integer literal of value less or equal to the maximum repetition depth that the `$ident` metavariable appears in.
- The expression expands to an unsuffixed integer literal token.

The `count(ident)` expression defaults `depth` to the maximum valid depth, making it count the total repetitions for the given metavariable.
The `count($ident)` expression defaults `depth` to the maximum valid depth, making it count the total repetitions for the given metavariable.

```rust,ignore
# // This code block marked `ignore` because mdbook can't handle `#![feature(...)]`.
#![feature(macro_metavar_expr)]

macro_rules! foo {
( $( $outer:ident ( $( $inner:ident ),* ) ; )* ) => {
println!("count(outer, 0): $outer repeats {} times", ${count(outer)});
println!("count(inner, 0): The $inner repetition repeats {} times in the outer repetition", ${count(inner, 0)});
println!("count(inner, 1): $inner repeats {} times in the inner repetitions", ${count(inner, 1)});
println!("count(outer, 0): $outer repeats {} times", ${count($outer)});
println!("count(inner, 0): The $inner repetition repeats {} times in the outer repetition", ${count($inner, 0)});
println!("count(inner, 1): $inner repeats {} times in the inner repetitions", ${count($inner, 1)});
};
}

Expand Down Expand Up @@ -164,11 +164,11 @@ fn main() {
}
```

## `ignore(ident)`
## `ignore($ident)`

The `ignore(ident)` metavariable expression expands to nothing, making it possible to expand something as often as a metavariable repeats without expanding the metavariable.
The `ignore($ident)` metavariable expression expands to nothing, making it possible to expand something as often as a metavariable repeats without expanding the metavariable.

- The `ident` argument must be a declared metavariable in the scope of the rule.
- The `$ident` argument must be a declared metavariable in the scope of the rule.

```rust,ignore
# // This code block marked `ignore` because mdbook can't handle `#![feature(...)]`.
Expand All @@ -181,7 +181,7 @@ macro_rules! repetition_tuples {
(
${index()},
${index(1)}
${ignore(inner)} // without this metavariable expression, compilation would fail
${ignore($inner)} // without this metavariable expression, compilation would fail
),
)*
)*)
Expand Down
Loading