参数类型可能活不够长?

时间:2015-04-20 06:16:58

标签: rust

以下代码段给出了一个错误:

use std::rc::Rc;

// Definition of Cat, Dog, and Animal (see the last code block)
// ...

type RcAnimal = Rc<Box<Animal>>;
fn new_rc_animal<T>(animal: T) -> RcAnimal
where
    T: Animal, /* + 'static */ // works fine if uncommented
{
    Rc::new(Box::new(animal) as Box<Animal>)
}

fn main() {
    let dog: RcAnimal = new_rc_animal(Dog);
    let cat: RcAnimal = new_rc_animal(Cat);
    let mut v: Vec<RcAnimal> = Vec::new();
    v.push(cat.clone());
    v.push(dog.clone());
    for animal in v.iter() {
        println!("{}", (**animal).make_sound());
    }
}
error[E0310]: the parameter type `T` may not live long enough
 --> src/main.rs:8:13
  |
4 | fn new_rc_animal<T>(animal: T) -> RcAnimal
  |                  - help: consider adding an explicit lifetime bound `T: 'static`...
...
8 |     Rc::new(Box::new(animal) as Box<Animal>)
  |             ^^^^^^^^^^^^^^^^
  |
note: ...so that the type `T` will meet its required lifetime bounds
 --> src/main.rs:8:13
  |
8 |     Rc::new(Box::new(animal) as Box<Animal>)
  |             ^^^^^^^^^^^^^^^^

但是编译得很好:

use std::rc::Rc;

// Definition of Cat, Dog, and Animal (see the last code block)
// ...

fn new_rc_animal<T>(animal: T) -> Rc<Box<T>>
where
    T: Animal,
{
    Rc::new(Box::new(animal))
}

fn main() {
    let dog = new_rc_animal(Dog);
    let cat = new_rc_animal(Cat);
}

错误的原因是什么?唯一真正的区别似乎是使用运算符as类型如何不能长寿? (playground

// Definition of Cat, Dog, and Animal
trait Animal {
    fn make_sound(&self) -> String;
}

struct Cat;
impl Animal for Cat {
    fn make_sound(&self) -> String {
        "meow".to_string()
    }
}

struct Dog;
impl Animal for Dog {
    fn make_sound(&self) -> String {
        "woof".to_string()
    }
}

1 个答案:

答案 0 :(得分:16)

实际上有很多类型可能“活得不够长”:所有具有生命周期参数的类型。

如果我要介绍这种类型:

struct ShortLivedBee<'a>;
impl<'a> Animal for ShortLivedBee<'a> {}

ShortLivedBee在任何生命周期内都无效,但也只对'a生效的有效。

所以在你的情况下绑定

where T: Animal + 'static

我可以为您的功能提供的唯一ShortLivedBeeShortLivedBee<'static>

导致这种情况的原因是,在创建Box<Animal>时,您正在创建一个特征对象,该对象需要具有关联的生命周期。如果您不指定它,则默认为'static。所以你定义的类型实际上是:

type RcAnimal = Rc<Box<Animal + 'static>>;

这就是为什么您的功能需要'static绑定添加到T的原因:ShortLivedBee<'a>外,Box<Animal + 'static>无法存储'a = 'static 1}}


另一种方法是为RcAnimal添加生命周期注释,如下所示:

type RcAnimal<'a> = Rc<Box<Animal + 'a>>;

将你的功能改为明确终身关系:

fn new_rc_animal<'a, T>(animal: T) -> RcAnimal<'a>
        where T: Animal + 'a { 
    Rc::new(Box::new(animal) as Box<Animal>)
}