首页 文章

“对于具有泛型类型的结构的向量,”特性`core :: marker :: Sized`未实现“

提问于
浏览
0

我试图在一个泛型中使用 Any 类型的结构中设置一个值,我将在以后用它来写入redis .

struct Property<T> {
  value: T,
}
struct Process {
  properties: Option<[Property<Any>]>,
}

这会返回一个错误:

the trait `core::marker::Sized` is not implemented for the type `[Property<core::any::Any + 'static>]`

Edit

阅读评论中的所有链接后,我想解释一下,我希望有一个可以接受任何原始类型作为值的属性:

use std::any::*;

struct Property<T> {
    value: T,
}

struct Process {
    properties: Option<Property<Any>>,
}

fn main() {
    let p = Process {
            properties: Some(
                Property::<String>{
                    value: ""
                }
            )
        };

    let p2 = Process {
            properties: Some(
                Property::<u32>{
                    value: 150
                }
            )
        };
}

1 回答

  • 0

    您可以使用向量代替:

    struct Process {
      properties: Option<Vec<Property<Any>>>,
    }
    

    错误说, core::marker::Sized 未实现,因此在编译时不知道大小 .

    有关数组和向量之间区别的更多信息,请参见here .

相关问题