"match" on an Arc outside pattern

I have a variable which is an Arc. I want to do a match on the cases of the enum. If I de-reference the enum in the match, the match syntax implies a copy, which isn't allowed because the item has a mutex inside and isn't copyable. I tried putting "as ref" in the pattern's data field, but that's not allowed either.

If I don't de-reference the enum, I have to write match patterns with an Arc enclosing each enum type. Is that even possible? The obvious syntax doesn't work.

This kind of destructuring doesn't seem to be covered in the documentation.
Either this is easy, or not supported, I suspect.

(Use case: refactoring a big program, which included putting a mutex inside something that previously was copyable.)

You can match on references, and any binding will also become a reference:

match &*chooser {
    Choices::Red(item) => println!("Red {}", item.datum.read().unwrap()),
    Choices::Green => println!("Green")
}

This is basically sugar for ref, though I would generally avoid it since most people are not familiar with it:

match *chooser {
    Choices::Red(ref item) => println!("Red {}", item.datum.read().unwrap()),
    Choices::Green => println!("Green")
}
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.