How to create a structure with a link vector?

Rust seems such an optimal language - I should have known that fighting a compiler would be a cost. I am trying to create a constructor for a structure and I am getting errors does not live long enough.

Distilled to the bone, I have:

fn main() {
    println!("RUST PLEASE");
}

struct Help<'a> {
    list: Vec<&'a Traits>
}

impl<'a> Help<'a> {
    fn new() -> Help<'a> {
        Help { list: vec![&Test] }
    }
}

trait Traits {
    fn mice(&self);
}

struct Test;
impl Traits for Test {
    fn mice(&self) { print!("WHY"); }
}

So, I am doing this because this vector listshould contain a list of feature objects. It (presumably) heterogeneously rests on any class that implements the provided attribute. As far as I understand, this means that I have to use links so that the vector has a real size to work. And since this is a reference, life must be there to ensure that they live and die together.

, , . new, Test new, . , Test , . , ? Help? , .

, - , , .

EDIT , ; , . , , , - , .

+4
1

, , ... :

fn main() {
    println!("RUST PLEASE");
}

struct Help {
    list: Vec<Box<Traits>>
}

impl Help {
    fn new() -> Help {
        Help { list: vec![Box::new(Test)] }
    }
}

trait Traits {
    fn mice(&self);
}

struct Test;
impl Traits for Test {
    fn mice(&self) { print!("WHY"); }
}

Help , .

Playpen

+3

All Articles