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` 3Related 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