[英]Parameter type may not live long enough
我有一个简单的程序,我试图实现多态帐户类型:
enum AccountType {
INVALID,
TYPE1,
TYPE2,
}
trait Account {
fn get_name(&self) -> String;
fn get_type(&self) -> AccountType;
}
struct Accounts {
accounts: Vec<Box<Account>>,
}
impl Accounts {
fn new() -> Accounts {
let accs: Vec<Box<Account>> = Vec::new();
Accounts { accounts: accs }
}
fn add_account<A: Account>(&self, account: A) {
self.accounts.push(Box::new(account));
}
}
fn main() {
let accounts = Accounts::new();
}
( Rust Playground )
当我编译它时,我看到以下错误:
error[E0310]: the parameter type `A` may not live long enough
--> src/main.rs:23:28
|
22 | fn add_account<A: Account>(&self, account: A) {
| -- help: consider adding an explicit lifetime bound `A: 'static`...
23 | self.accounts.push(Box::new(account));
| ^^^^^^^^^^^^^^^^^
|
note: ...so that the type `A` will meet its required lifetime bounds
--> src/main.rs:23:28
|
23 | self.accounts.push(Box::new(account));
| ^^^^^^^^^^^^^^^^^
我试过在类型中添加生命周期,但找不到正确的方法。 如果这不是在Rust中进行多态性的正确方法,请告诉我。
我将尝试给出一个更彻底的答案:该问题与accounts
成员Accounts
的定义有关。 在此上下文中, Vec<Box<Account>>
等同于Vec<Box<Account + 'static>>
,即该框不能包含对堆栈上数据的任何引用。 另一方面, add_account
的声明不限制类型的生命周期:它相当于fn add_account<'a, A: Account + 'a>(&self, account: A) {
。
解决方案是确保A型足够长。 最简单的方法是在错误消息中添加A: 'static
绑定( fn add_account<A: Account + 'static>(&self, account: A) {
)。
如果您不想复制帐户数据,可以执行更复杂的操作,如下所示:
struct Accounts<'a> {
accounts: Vec<&'a Account + 'a>
}
impl<'a> Accounts<'a> {
fn new() -> Accounts<'a> {
Accounts { accounts: Vec::new() }
}
fn add_account<A: Account + 'a>(&mut self, account: &'a A) {
self.accounts.push(Box::new(account));
}
}
但是,在这一点上,您的数据结构可能比实际需要的更为通用。
编译器的建议确实有效。 如果您按如下方式编写add_account
:
fn add_account<A: Account + 'static>(&mut self, account: A) {
self.accounts.push(Box::new(account));
}
你的代码编译。 (顺便说一句,你需要&mut self
,而不是&self
here)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.