这基本上归结为,你不允许
attributes
在这样的表达式中,例如,以下内容无效:
b_slice.iter()
#[cfg(not(feature = "threaded"))]
.map(|x| x)
.collect();
请注意双精度
{{
和
}}
block
#[macro_export]
macro_rules! par_iter {
($($tokens:tt)*) => {{
#[cfg(feature = "threaded")]
let it = $($tokens)*.par_iter();
#[cfg(not(feature = "threaded"))]
let it = $($tokens)*.iter();
it
}};
}
或者,也可以将其拆分为两个宏,如下所示:
#[cfg(feature = "threaded")]
#[macro_export]
macro_rules! par_iter {
($($tokens:tt)*) => {
$($tokens)*.par_iter()
}
}
#[cfg(not(feature = "threaded"))]
#[macro_export]
macro_rules! par_iter {
($($tokens:tt)*) => {
$($tokens)*.iter()
}
}