macro_rules! replace_pattern {
($a:pat = $at:ident => $b:pat = $bt:ident) => { ... };
}
Expand description
Replaces a pattern with another pattern. A placeholder is required in order to specify the types of the search and replacement pattern. Although they can be the same if you’re replacing a pattern with another of the same type.
§Examples
fn some_foo_to_result_foo(p1: Option<Foo>, p2: Result<Foo, ()>) {
replace_pattern!(Some(f1) = p1 => Result::Ok(f1) = p2);
replace_pattern!(None = p1 => Result::Err(()) = p2);
}
This will transform:
match f() {
Some(Foo(x)) => x,
None => 0,
}
Into:
match f() {
Result::Ok(Foo(x)) => x,
Result::Err(()) => 0,
}