用非常简单的术语来说,什么是终身省略? [英] What is lifetime elision in very simple terms?

查看:23
本文介绍了用非常简单的术语来说,什么是终身省略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Rust 文档:

Rust 在函数体中支持强大的局部类型推断,但它故意不对项签名的类型进行任何推理.但是,出于人体工程学的原因,在判断生命周期时确实适用了一种非常受限制的二次推理算法,称为生命周期省略".生命周期省略仅涉及使用三个易于记忆且明确的规则来推断生命周期参数.这意味着生命周期省略可以作为编写项目签名的速记,同时不会像完全本地推理那样隐藏所涉及的实际类型.

Rust supports powerful local type inference in the bodies of functions, but it deliberately does not perform any reasoning about types for item signatures. However, for ergonomic reasons, a very restricted secondary inference algorithm called "lifetime elision" does apply when judging lifetimes. Lifetime elision is concerned solely with inferring lifetime parameters using three easily memorizable and unambiguous rules. This means lifetime elision acts as a shorthand for writing an item signature, while not hiding away the actual types involved as full local inference would if applied to it.

我不明白这是什么意思.什么是项目签名?推断生命周期参数"是什么意思?一些例子或类比会有所帮助.

I don't understand what this means. What are item signatures? What does "infer lifetime parameters" mean? Some examples or analogies would be helpful.

推荐答案

项签名是给出函数名称和类型的位,即您需要调用它的所有内容(无需知道它是如何实现的);例如:

An item signature is the bit which gives the name and types of your function, i.e. everything you need to call it (without needing to know how it's implemented); for example:

fn foo(x: u32) -> u32;

这是另一个需要 &str 引用的:

Here's another which takes a &str reference:

fn bar<'a>(s: &'a str) -> &'a str;

在 Rust 中,所有引用都有一个附加的生命周期;这是类型的一部分.上面的 bar 函数不仅仅是这个函数引用一个字符串并返回另一个字符串".它说这个函数接受一个字符串引用,并返回另一个只要它给定的那个就有效.这是 Rust 所有权系统的重要组成部分.

In Rust, all references have an attached lifetime; this is part of the type. The above bar function says more than just "this function takes a reference to a string and returns another one". It says "this function takes a string reference, and returns another which is valid for as long as the one it's given. This is an important part of Rust's ownership system.

然而,每次都指定这些生命周期很烦人也很痛苦,所以 Rust 有生命周期省略"(即没有明确地写出它们").这意味着对于一些非常常见的情况,您可以将生命周期注解留在外面,Rust 会为您隐式添加它们.这纯粹是为了程序员的方便,这样他们就不必在明显"的情况下编写这么多生命周期.

However, it's annoying and a pain to specify these lifetimes every time, so Rust has "lifetime elision" (i.e. "not explicitly writing them out"). All that means is that for a few very common cases, you can leave the lifetime annotations out and Rust will implicitly add them for you. This is purely a convenience for programmers so that they don't have to write so many lifetimes in "obvious" cases.

规则列在本书中,但对于完整性:

The rules are listed in the book, but for completeness they are:

  1. 未另外指定的函数参数中的每个生命周期都是不同的.例如:

fn f(x: &T, y: &U)

意思是:

fn f<'a, 'b>(x: &'a T, y: &'b U)

即这些生命周期之间没有自动联系.

i.e. there's no automatic link between those lifetimes.

  1. 如果只有一个输入生命周期,它会用于每个输出生命周期.例如:

struct U<'a> {}  // struct with a lifetime parameter

fn f(x: &T) -> &U

变成:

fn f<'a>(x: &'a T) -> &'a U<'a>

  1. 否则,如果有多个输入生命周期,但其中一个是 &self&mut self(即它是一种方法),则所有省略的输出生命周期与 self 相同.这涵盖了方法返回对其字段之一的引用的常见情况.例如:
  1. Otherwise, if there are multiple input lifetimes but one of them is &self or &mut self (i.e. it's a method), then all the elided output lifetimes get the same as self. This covers the common case that a method returns a reference to one of its fields. For example:

impl S {
    fn get_my_item(&self, key: &str) -> &str {}
}

变成:

fn get_my_item<'a,'b>(&'a self, key: &'b str) -> &'a str  // use the self lifetime

文档中有更多示例.

这篇关于用非常简单的术语来说,什么是终身省略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆