The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want

前端 未结 2 670
[愿得一人]
[愿得一人] 2020-11-22 09:35

I\'m trying to implement something that looks like this minimal example:

trait Bar {}

struct Foo {
    data: Vec>         


        
2条回答
  •  死守一世寂寞
    2020-11-22 10:11

    asking me to consider adding a 'static lifetime qualifier (E0310). I am 99% sure that's not what I want, but I'm not exactly sure what I'm supposed to do.

    Yes it is. The compiler does not want a &'static reference, it wants U: 'static.

    Having U: 'static means that U contains no references with a lifetime less than 'static. This is required because you want to put a U instance in a structure without lifetimes.

    trait Bar {}
    
    struct Foo {
        data: Vec>>,
    }
    
    impl Foo {
        fn add + 'static>(&mut self, x: U) {
            self.data.push(Box::new(x));
        }
    }
    

提交回复
热议问题