我在枚举上有一个匹配块,它的一个匹配案例在同一个枚举上包含另一个匹配块。像这样的东西:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match self {
Scenario::Step { attributes, .. } => {
match scenario {
Scenario::Step { attributes,.. } => {
有没有办法访问attributes
内部匹配中的两个字段?我看到了从内部匹配块返回该字段的可能性,但是可以以更美观的方式处理它吗?
您可以像这样重命名匹配的变量:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match self {
Scenario::Step { attributes: attrs1, .. } => {
match scenario {
Scenario::Step { attributes: attrs2,.. } => {
// do something with attrs1 and attrs2
更好的是,您可以在元组中匹配它们:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match (self, scenario) {
(Scenario::Step { attributes: attrs1, .. }, Scenario::Step { attributes: attrs2,.. }) => {
// do something with attrs1 and attrs2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句