Is it possible to get the enum field directly according to the generic type

There's no need for unsafe; trait Any provides downcast_ref to encapsulate the pattern.

    pub fn get<T: 'static>(&self) -> Option<&T> {
        let x: &dyn Any = match self {
            Self::A(x) => x,
            Self::B(x) => x,
            Self::C(x) => x,
        };
        x.downcast_ref()
    }
6 Likes