返回

rust-获取枚举变量中的值

发布时间:2022-08-23 21:35:43 684
# 前端

我试图得到enum有许多新类型,如这样(只有两种要简化):

enum NewType {
    Integer(i32),
    Null,
}

impl From for NewType {
    fn from(int: i32) -> Self {
        Self::Integer(int)
    }
}

impl NewType {
    fn into_inner(&self) -> Option
        where
            T: Into,
        {
        match self {
            NewType::Integer(v) => Some(*v),
            NewType::Null => None,
        }
    }
}

但我得到:

mismatched types
expected type parameter `T`
             found type `i32`

尝试时:

impl From for T {
    fn from(new_value: NewType) -> Option 
    where
        T: Into,
    {
        match new_value {
            NewType::Integer(v) => Some(v),
            NewType::Null => None,
        }
    }
}

我得到:

type parameter `T` must be covered by another type when it appears before the first local type (`NewType`)
implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type

在生锈中获得这些内在价值的最佳方法是什么?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像