Rust生命周期bound用于泛型的引用

栏目: 编程语言 · Rust · 发布时间: 6年前

内容简介:Rust在实际编程中,可能会出现泛型引用这种情况,我们会编写如下的代码:会产生编译错误:为什么会编译错误呢?因为

Rust在实际编程中,可能会出现泛型引用这种情况,我们会编写如下的代码:

struct Inner<'a, T> {
    data: &'a T,
}

会产生编译错误:

error[E0309]: the parameter type `T` may not live long enough
  --> src/main.rs:16:5
   |
15 | struct Inner<'a, T> {
   |                  - help: consider adding an explicit lifetime bound `T: 'a`...
16 |     data: &'a T,
   |     ^^^^^^^^^^^
   |
note: ...so that the reference type `&'a T` does not outlive the data it points at
  --> src/main.rs:16:5
   |
16 |     data: &'a T,
   |     ^^^^^^^^^^^

为什么会编译错误呢?因为 T 可以是任意类型, T 自身也可能是一个引用,或者是一个存放了一个或多个引用的类型,而他们各自可能有着不同的生命周期。Rust编译器不能确认 T 会与 'a 存活的一样久。

所以编译器才提示我们: T 的生命周期可能不够长,建议我们对泛型 T 进行生命周期bound,限定 T 的生命周期与 'a 一样长。

之后我们根据编译器的提示更改代码:

trait Print {
    fn output(&self);
}

struct Object<'a>{
    data: &'a i32,
}

impl<'a> Print for Object<'a> {
    fn output(&self) {
        println!("Object data: {}", self.data);
    }
}

struct Inner<'b, T: 'b + Print> {
    pub a: &'b T,
}

fn main() {
    let data = 10;
    {
        let a = Object{data: &data};
        let inner = Inner{a: &a};
        inner.a.output();
    }
}

对于生命周期bound,可以结合 trait bound 来理解。Rust的泛型有个特点就是 trait boundtrait bound 可以对泛型进行某些限制(只有实现了指定trait的类型才符合要求)。同样,我们也可以像泛型那样为生命周期参数增加限制,这被称为“生命周期bound”(lifetime bounds)。生命周期bound帮助Rust编译器验证泛型的引用不会存在的比其引用的数据更久。

上面的代码中, struct Inner<'b, T: 'b + Print> 这行代码就是表示对泛型 T 同时进行trait bound和生命周期bound。

Rust通过生命周期参数注解引用来帮助编译器理解不同引用的生命周期如何相互联系。从而使编译器能够判断引用是否有效。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Apache Modules Book

The Apache Modules Book

Nick Kew / Prentice Hall PTR / 2007-02-05 / USD 54.99

"Do you learn best by example and experimentation? This book is ideal. Have your favorite editor and compiler ready-you'll encounter example code you'll want to try right away. You've picked the right......一起来看看 《The Apache Modules Book》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具