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

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

Open
eric642 opened this issue Jun 28, 2022 · 3 comments
Open

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

eric642 opened this issue Jun 28, 2022 · 3 comments

Comments

@eric642
Copy link

eric642 commented Jun 28, 2022

在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
Copy link
Author

eric642 commented Jun 28, 2022

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

@zjp-CN
Copy link
Owner

zjp-CN commented Jun 28, 2022

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

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

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

@eric642
Copy link
Author

eric642 commented Jun 29, 2022

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

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

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

好的, 多谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants