List 如何在Rust中创建泛型结构的集合?

List 如何在Rust中创建泛型结构的集合?,list,generics,collections,rust,List,Generics,Collections,Rust,我正在创建一个基于文本的《铁锈》垄断游戏作为个人项目。我当前的设置是将各种类型的空间作为它们自己的结构(例如,Property) 以及泛型Space结构,其中包含一种类型的空格的实例(例如Space或Space) 不幸的是,我在创建此集合时遇到了一个问题。无论我尝试哪一个,它都告诉我必须为空格指定一个类型参数。如何创建泛型结构的集合?如果我可以通过索引提取某个特定空间,这将更为可取,因为当我开始执行某些Chance卡时,它将使您稍后移动到特定空间。抱歉,如果我遗漏了一些明显的东西,我是新手,所以

我正在创建一个基于文本的《铁锈》垄断游戏作为个人项目。我当前的设置是将各种类型的空间作为它们自己的结构(例如,
Property

以及泛型
Space
结构,其中包含一种类型的空格的实例(例如
Space
Space


不幸的是,我在创建此集合时遇到了一个问题。无论我尝试哪一个,它都告诉我必须为空格指定一个类型参数。如何创建泛型结构的集合?如果我可以通过索引提取某个特定空间,这将更为可取,因为当我开始执行某些
Chance
卡时,它将使您稍后移动到特定空间。抱歉,如果我遗漏了一些明显的东西,我是新手,所以我不一定总是知道要查找什么。

Rust编译器需要知道Vec中的每个元素有多大,才能决定如何布局内存(每个元素必须占用相同的空间)。您的
Space
结构有一个类型参数
T
,因此不清楚
Space
将占用多少空间,除非您也提供类型参数。例如,
Vec
将起作用

如果需要大小不同的对象的
Vec
,常用的方法是使用向量。在您的示例中,可以有一个
Vec
,一个实现
事件
特征的对象向量,这里的每个Vec元素都是一个框(智能指针),指向实现
事件
的堆分配类型

pub trait Event {
    fn event(&self);
}

pub struct Property {
    message: String,
}

impl Property {
    pub fn new(message: String) -> Property {
        Property { message }
    }
}

impl Event for Property {
    fn event(&self) {
        println!("{}", &self.message);
    }
}

pub struct Utility {
    message: String,
}

impl Utility {
    pub fn new(message: String) -> Utility {
        Utility { message }
    }
}

impl Event for Utility {
    fn event(&self) {
        println!("{}", &self.message);
    }
}

pub struct Game {
    pub spaces: Vec<Box<Event>>,
}

fn main () {
    let game = Game{ 
        spaces: vec![
            Box::new(Utility::new(String::from("Water works"))),
            Box::new(Property::new(String::from("Fleet Street"))),
            Box::new(Utility::new(String::from("Electric company"))),
            Box::new(Property::new(String::from("Bond Street"))),
        ] 
    };

    for space in game.spaces {
        space.event();
    }
}

// Water works
// Fleet Street
// Electric company
// Bond Street
pub-trait事件{
fn事件(和自我);
}
发布结构属性{
消息:String,
}
impl属性{
pub fn new(消息:String)->属性{
属性{message}
}
}
属性的impl事件{
fn事件(&self){
println!({},&self.message);
}
}
发布结构实用程序{
消息:String,
}
impl实用程序{
pub fn new(消息:String)->实用程序{
实用程序{message}
}
}
实用程序的impl事件{
fn事件(&self){
println!({},&self.message);
}
}
酒吧结构游戏{
酒吧空间:Vec,
}
fn main(){
让游戏=游戏{
空间:vec[
框::新(实用工具::新(字符串::来自(“水厂”)),
Box::new(属性::new(字符串::from(“Fleet Street”)),
框::新(实用工具::新(字符串::来自(“电气公司”)),
框::新(属性::新(字符串::来自(“邦德街”)),
] 
};
游戏中的空间{
space.event();
}
}
//水厂
//舰队街
//电力公司
//邦德街

注意:在Rust 2018中,我们更喜欢在用作trait对象的trait前面加上
dyn
关键字。所以
Vec
会更地道:)谢谢!我还不知道智能指针,但你的解释很有帮助!
pub trait Event {
    fn event(&self);
}

pub struct Space<T> {
    item: T,
}

impl<T: Event> Space<T> {
    pub fn new(item: T) -> Space<T> {
        Space { item: item }
    }

    pub fn event(&self) {
        &self.item.event();
    }
}
pub struct Game {
    spaces: Vec<Space>, // Does not work
}
pub trait Event {
    fn event(&self);
}

pub struct Property {
    message: String,
}

impl Property {
    pub fn new(message: String) -> Property {
        Property { message }
    }
}

impl Event for Property {
    fn event(&self) {
        println!("{}", &self.message);
    }
}

pub struct Utility {
    message: String,
}

impl Utility {
    pub fn new(message: String) -> Utility {
        Utility { message }
    }
}

impl Event for Utility {
    fn event(&self) {
        println!("{}", &self.message);
    }
}

pub struct Game {
    pub spaces: Vec<Box<Event>>,
}

fn main () {
    let game = Game{ 
        spaces: vec![
            Box::new(Utility::new(String::from("Water works"))),
            Box::new(Property::new(String::from("Fleet Street"))),
            Box::new(Utility::new(String::from("Electric company"))),
            Box::new(Property::new(String::from("Bond Street"))),
        ] 
    };

    for space in game.spaces {
        space.event();
    }
}

// Water works
// Fleet Street
// Electric company
// Bond Street