Parameter type may not live long enough

前端 未结 2 1235
失恋的感觉
失恋的感觉 2021-01-14 05:24

I have a simple program where I am trying to implement a polymorphic account type:

enum AccountType {
    INVALID,
    TYPE1,
    TYPE2,
}

trait Account {
          


        
2条回答
  •  借酒劲吻你
    2021-01-14 05:45

    The compiler's suggestion actually works. If you write add_account as follows:

    fn add_account(&mut self, account: A) {
        self.accounts.push(Box::new(account));
    }
    

    your code compiles. (Incidentally, you need &mut self, not &self here)

提交回复
热议问题