2021年1月4日星期一

Why does the following macro expect a semi-colon when called?

I'm trying to write a macro to switch between rayon's par_iterand std'siter` depending on a build feature (possibly getting ahead of myself, as I've not read a lot on macros yet). A macro seems a bit better to deal with than a function here, since a function might need some relatively complex types to make this work; as well a macro might remain more flexible in the future if I wanted to add even more variations in build features pertaining to how to run iterators.

#[macro_export]  macro_rules! par_iter {      ($($tokens:tt)*) => {        #[cfg(feature = "threaded")]        $($tokens)*.par_iter()        // #[cfg(not(feature = "threaded"))]        $($tokens)*.iter()      }  }  

I see the following error:

error: macro expansion ignores token `b_slice` and any following     --> src/util.rs:28:8                                                                            |   28  |       $($tokens)*.iter();      |        ^^^^^^^^^      |                                                                                             ::: src/counting.rs:219:9                                                                       |  219 |         par_iter!(b_slice).map(WordCount::from)                                                                                                                                           |         ------------------- help: you might be missing a semicolon here: `;`      |         |                                                                                    |         caused by the macro expansion here      |      = note: the usage of `par_iter!` is likely invalid in expression context  

While I have no idea about the first error, I'm curious why a ; is expected - how do I make it valid in expression context?

https://stackoverflow.com/questions/65572271/why-does-the-following-macro-expect-a-semi-colon-when-called January 05, 2021 at 10:05AM

没有评论:

发表评论