预期用途:
cond! {
x > 5 => 0,
x < 3 => 1,
true => -1
}
应扩展为:
if x > 5 { 0 } else if x < 3 { 1 } else if true { -1 }
请注意,它不会产生包罗万象的else { ... }
后缀。
我的尝试:
macro_rules! cond(
($pred:expr => $body:expr) => {{
if $pred {$body}
}};
($pred:expr => $body:expr, $($preds:expr => $bodies:expr),+) => {{
cond! { $pred => $body } else cond! { $($preds => $bodies),+ }
}};
);
但是,编译器抱怨该else
关键字。
error: expected expression, found keyword `else`
--> src/main.rs:32:34
|
32 | cond! { $pred => $body } else cond! { $($preds => $bodies),+ }
| ^^^^