Reputation: 67
I have a match block on enum, and one of it`s match cases contains another match block on the same enum. Something like this:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self>
{
match self {
Scenario::Step { attributes, .. } => {
match scenario {
Scenario::Step { attributes,.. } => {
Is there any way to access both attributes
field inside of the inner match? I see possibility to just return that field from the inner match block, but can it be handled in more beauty way?
Upvotes: 5
Views: 1833
Reputation: 42678
You could match both of them at the same time in a tuple:
fn foo(&mut self, scenario: &mut Scenario) -> Result<&mut Self> {
match (self, escenario) {
(Scenario::Step { attributes, .. }, Scenario::Step { attributes: attributes2, .. }) => {}
}
}
Upvotes: 1
Reputation: 6651
You can rename the matched variable like this:
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
And even nicer, you could match on them in a tuple:
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
Upvotes: 8