I am trying to pattern match on an enum when getting an item from my dashmap::DashMap. However, it looks like they have a wrapper type over the Entity when they return the data. How can I pattern match over the items then?

use once_cell::sync::Lazy; use dashmap::DashMap; enum Entity { Person { name: String }, Animal { name: String }, } static ENTITIES: Lazy<DashMap<usize, Entity>> = Lazy::new(|| DashMap::new()); fn main() { ENTITIES.insert( 0, Entity::Animal { name: "pikachu".into(), }, ); ENTITIES.insert( 1, Entity::Person { name: "trainer mike".into(), }, ); match ENTITIES.get(&0) { Some(Entity::Animal { name }) => { // compile error here println!("found animal: {}", name); } _ => panic!("did not find person"), } } 

And the error:

error[E0308]: mismatched types --> src\lib.rs:__:__ | | match ENTITIES.get(&0) { | -------------- this expression has type `Option<dashmap::mapref::one::Ref<'_, usize, Entity>>` | Some(Entity::Animal { name }) => { | ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `dashmap::mapref::one::Ref`, found enum `Entity` | = note: expected struct `dashmap::mapref::one::Ref<'_, usize, Entity, >` found enum `Entity` 
3

Related questions 3 Match pattern from vector 0 Is it possible to use pattern matching in the map function in swift? 3 Pattern matching over borrowed HashMap containing enums Related questions 3 Match pattern from vector 0 Is it possible to use pattern matching in the map function in swift? 3 Pattern matching over borrowed HashMap containing enums 3 How can I pattern match on a vector of string slices and extract values? 5 Match tuple as input to map 14 What is a good way to match strings against patterns and extract values? 0 Pattern match against the other element of a map 1 How can I best pattern match in Result::map 9 How do I match based on a dynamic variable? 1 Rust: How to Match against Any Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.