简体   繁体   English

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

[英]“the trait `core::marker::Sized` is not implemented” for a vector of structs with a generic type

I'm trying to set a value in a struct with an Any type in a generic, i'm going to use it later for writing to redis. 我正在尝试在泛型中使用Any类型的结构中设置一个值,我稍后将使用它来写入redis。

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

This returns an error: 这将返回错误:

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

Edit 编辑

After reading all the links from the comment, I would like to explain that I would love to have a property that can accept any primitive type as a value: 阅读完注释中的所有链接之后,我想解释一下,我希望拥有一个可以接受任何原始类型作为值的属性:

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
                }
            )
        };
}

You could use a vector instead: 您可以改用向量:

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

The error says, that core::marker::Sized is not implemented, so the size is not known at compile time. 该错误表明,未实现core::marker::Sized ,因此在编译时未知大小。

See here for little more information about the distinction between arrays and vectors. 有关数组和向量之间区别的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM