最佳答案
我有这样的东西(真正的函数是 锈印中的 Ini::Section::get
) :
impl Foo {
pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str>
where
K: Hash + Eq,
{
// ...
}
}
我得打好几次电话:
fn new() -> Result<Boo, String> {
let item1 = match section.get("item1") {
None => return Result::Err("no item1".to_string()),
Some(v) => v,
};
let item2 = match section.get("item2") {
None => return Result::Err("no item2".to_string()),
Some(v) => v,
};
}
为了消除代码膨胀,我可以编写如下宏:
macro_rules! try_ini_get {
($e:expr) => {
match $e {
Some(s) => Ok(s),
None => Err("no ini item".to_string()),
}
}
}
有没有办法在没有这个宏实现的情况下删除代码复制?