Skip to content

关于count(ident) 是 count(ident, 0) 的简写的问题 #7

@eric642

Description

@eric642

在2.3.3章中,我使用count示例去测试: 如下是我的代码:

#![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)});
        println!("count(inner, 1): $inner repeats {} times in the inner repetitions", ${count(inner, 1)});
    };
}

fn main() {
    foo! {
        outer () ;
        outer ( inner , inner ) ;
        outer () ;
        outer ( inner ) ;
    };
}

这时候的输出是:
count(outer, 0): $outer repeats 4 times
count(inner, 0): The $inner repetition repeats 3 times in the outer repetition
count(inner, 1): $inner repeats 3 times in the inner repetitions
而如果我把${count(inner)改为${count(inner, 0)后, 输出变成了:
count(outer, 0): $outer repeats 4 times
count(inner, 0): The $inner repetition repeats 4 times in the outer repetition
count(inner, 1): $inner repeats 3 times in the inner repetitions
我的版本是:
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active rustc version is rustc 1.64.0-nightly (2f3ddd9f5 2022-06-27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions