美文网首页
Rust for cpp devs - 生命周期

Rust for cpp devs - 生命周期

作者: 找不到工作 | 来源:发表于2021-05-03 11:56 被阅读0次

生命周期(lifetime)也是一类泛型。但它是用于指定引用保持有效的作用域。

Rust 中的每个引用都有相应的 lifetime。大部分时候,lifetime 可以被编译器推断出来,无需手动指明,就像类型推断一样。同样的,当编译器无法确定 lifetime 时,Rust 要求开发者指明。主要目的就是在编译期保证不存在悬垂引用。

Generic Lifetimes in Functions

我们从一段简单的代码入手理解 lifetime。这是一个返回较长字符串的函数:

fn longest(x: &str, y: &str) -> &str {
    let longer = if x.len() > y.len() {x} else {y};
    return longer
}

fn main() {
    let s1 = String::from("abcd");
    let s2 = "xyz";
    let result = longest(s1.as_str(), s2);
    println!("The longer string is {}", result);
}

这是一个简单的函数,但是编译时会报错:

error[E0106]: missing lifetime specifier
 --> src/main.rs:1:33
  |
1 | fn longest(x: &str, y: &str) -> &str {
  |               ----     ----     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
help: consider introducing a named lifetime parameter
  |
1 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
  |           ^^^^    ^^^^^^^     ^^^^^^^     ^^^

其原因是 Rust 无法确定其 lifetime。这是由于 x y 都是来自于函数外的引用,编译器无法推断出参数和返回值的 lifetime。因此,我们需要手动标记参数和返回值的 lifetime。

什么是生命周期(非教材内容)

生命周期的表示方法是 ' 加生命周期的名字,一般接在引用符号 & 后。例如:

&i32        // a reference
&'a i32     // a reference with an explicit lifetime
&'a mut i32 // a mutable reference with an explicit lifetime

本节主要是一些补充知识,有助于更好地从本质理解 lifetime。

生命周期也是一种类型,长生命周期是短生命周期的子类型。 我们可以用 : 来标记这种父子关系。例如'b 长于 'a,可以记作 'b: 'a

生命周期以模板形式,为函数额外传入了变量的生命周期。

生命周期的标记不影响变量的生命周期。标记只是为编译器里的 borrow checker 明确该如何检查生命周期。

在函数签名中标记生命周期

我们实际上需要解决的问题是,已知 x 的 lifetime 是 'ay 的 lifetime 是 'b,那么返回值的 lifetime (假设是 'c)怎么通过 'a'b 表示?

fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &'c str {
    let longer = if x.len() > y.len() {x} else {y};
    return longer
}

显然,我们需要让 'c'a'b 的交集,即较小的那个。因此必然有 'a: 'c'b: 'c。既然 'a'b 都是 'c 的父类型,那么可以直接把参数中的 'a'b 替换为 'c

fn longest<'c>(x: &'c str, y: &'c str) -> &'c str {
    let longer = if x.len() > y.len() {x} else {y};
    return longer
}

这样写相当于告诉编译器,返回值的 lifetime 是参数 lifetime 的公共父类型,即交集。

明确了函数参数和返回值的 lifetime 后,编译器在函数调用者处就有了足够的信息来检验生命周期。

结构体定义中的 lifetime 标记

结构体也可以持有变量的“引用”而非”所有权“。例如:

struct ImportantExcerpt {
    part: &str,
}

但是这段代码无法编译,因为:

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:11
  |
7 |     part: &str,
  |           ^ expected named lifetime parameter
  |
help: consider introducing a named lifetime parameter
  |
6 | struct ImportantExcerpt<'a> {
7 |     part: &'a str,
  |

这是因为当结构体持有变量的引用时,我们需要指定 lifetime 标记:

struct ImportantExcerpt<'a> {
    part: &'a str,
}

其含义是,ImportantExcerpt 存在的生命周期必须包含于 part 引用的生命周期。

省略生命周期

我们已经知道在 Rust 中:

  • 每个引用都有对应的 lifetime
  • 使用引用时,需要指定 lifetime

但是,我们确实曾经写过没有标注生命周期的函数。例如:

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }

    &s[..]
}

在 Rust 1.0 之前,这段代码确实会编译失败。因为每个引用都需要一个显式指定的生命周期。后来,Rust 开发人员将可以推断 lifetime 的模式写进了编译器代码,对于这些模式,不再需要手动指定。

如果满足下面三个条件,Rust 就可以推断生命周期,无需手动指定,否则,Rust 会在编译时报错:

  1. 为每个引用参数赋予 lifetime

  2. 如果只有一个参数,这个参数的 lifetime 会作为所有返回值的 lifetime

  3. 如果有多个参数,但是其中之一是 &self&mut self(类方法),则返回值的 lifetime 与 self 一致

例如,对于 first_word 函数:

fn first_word(s: &str) -> &str {

编译器首先执行,为参数赋予生命周期 'a(Rule 1):

fn first_word(s: &'a str) -> &str {

由于只有一个参数,它的 lifetime 被直接赋值给返回值(Rule 2):

fn first_word<'a>(s: &'a str) -> &'a str {

现在所有引用都有了 lifetime,所以编译器不需要开发者指定 lifetime 了。

与之相对,对于 longest 函数:

fn longest(x: &str, y: &str) -> &str {

首先为每个输入赋予 lifetime (Rule 1):

fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &str {

Rule 2 不适用,因为有两个不同的输入 lifetime。

Rule 3 也不适用,因为 longest 不是一个类方法,不存在 self

因此编译器无法确定返回值的 lifetime,只能要求开发者指定。

生命周期与泛型

因为 lifetime 是一种泛型,所以 'aT 可以写在同一个 <> 中。

fn longest_with_an_announcement<'a, T: Display>(x: &'a str, y: &'a str, ann: T) -> &'a str {
    println!("Announcement! {}", ann);
    let longer = if x.len() > y.len() { x } else { y };
    return longer;
}

相关文章

  • Rust for cpp devs - 生命周期

    生命周期(lifetime)也是一类泛型。但它是用于指定引用保持有效的作用域。 Rust 中的每个引用都有相应的 ...

  • Rust for cpp devs - 线程

    由于 Rust 特有的 ownership 和类型检查机制,许多并发问题都可以在编译期发现,这极大地降低了风险以及...

  • Rust for cpp devs - mutex

    除了 channel[https://www.jianshu.com/p/925d3534ac7f],我们也可以通...

  • Rust for cpp devs - Ownership

    编程语言的内存管理一般有两种: 带垃圾回收机制的,如 Java,Golang,会在运行时检查不再使用的内存并回收,...

  • Rust for cpp devs - channel

    与 golang 一样,Rust 也实现了 channel 用于线程间的通信。如同 golang 的口号一样: D...

  • Rust for cpp devs - closure

    类似于 cpp 中的 lambda 表达式,Rust 中也有 closure。他们与普通函数的区别在于可以捕获变量...

  • Rust for cpp devs - Generic Type

    类似于 cpp,Rust 中也有泛型(generics),用于避免重复的代码。此外,还可以指定 traits 来限...

  • Rust for cpp devs - 迭代器

    迭代器(Iterator)可以允许对序列中的每一个元素执行某个操作。 Rust 的迭代器分为三种: iter() ...

  • Rust for cpp devs - minigrep 项目

    官方文档用 minigrep 项目来讲解如何组织一个 Rust 项目。 保持 main 函数简洁 这样做的好处是:...

  • Rust for cpp devs - 错误处理

    Rust 将软件中的错误分为两个类型:可恢复错误和不可恢复错误。 对于可恢复错误,例如文件找不到,可以报告给调用者...

网友评论

      本文标题:Rust for cpp devs - 生命周期

      本文链接:https://www.haomeiwen.com/subject/cpuzrltx.html