tlborm icon indicating copy to clipboard operation
tlborm copied to clipboard

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

Open eric642 opened this issue 2 years ago • 3 comments

在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)

eric642 avatar Jun 28 '22 09:06 eric642

count(inner, 0): The $inner repetition repeats 3 times in the outer repetition从3变成了4, 按道理不应该还是3吗

eric642 avatar Jun 28 '22 09:06 eric642

metavar 是不稳定的,而且我发现

${count(inner) 并不是 ${count(inner, 0) 的简写,而是最深嵌套那层的计数,也就是你这里的 ${count(inner, 1)

如果真的感兴趣,建议去看 https://github.com/rust-lang/rust/issues/83527#issuecomment-1070860171

zjp-CN avatar Jun 28 '22 13:06 zjp-CN

metavar 是不稳定的,而且我发现

${count(inner) 并不是 ${count(inner, 0) 的简写,而是最深嵌套那层的计数,也就是你这里的 ${count(inner, 1)

如果真的感兴趣,建议去看 rust-lang/rust#83527 (comment)

好的, 多谢

eric642 avatar Jun 29 '22 04:06 eric642