为什么结构中的切片需要生命周期,而不需要向量? [英] Why do slices in a structure require a lifetime, but not vectors?

查看:51
本文介绍了为什么结构中的切片需要生命周期,而不需要向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义以下结构时:

struct Test<'a> {
    a: &'a [i64],
    b: Vec<i64>,
}

切片和向量都包含一个指针.为什么切片需要生命周期,而向量不需要?

Both the slice and the vector contain a pointer. Why does the slice require a lifetime, but not the vector?

推荐答案

一个向量拥有它的元素.这意味着向量负责分配和释放它指向的元素.向量元素的生命周期与向量本身的生命周期相同,因此无需为 Vec 类型指定生命周期.

A vector owns its elements. That means the vector is responsible for allocating and freeing the elements it points to. The lifetime of the vector's elements is the same as the lifetime of the vector itself, so there's no need to specify a lifetime to the Vec type.

切片借用可以静态或动态分配的向量或数组的元素.切片必须指明借用元素的生命周期,以便编译器可以进行必要的安全检查.

A slice borrows the elements of a vector or array that may be allocated statically or dynamically. The slice must indicate the lifetime of the borrowed elements so that the compiler can make the necessary safety checks.

另一种表达方式是比较两个选项之间的事件顺序.

Another way to express this is by comparing the sequence of events between the two options.

对于向量:

  1. 分配了一个 Vec.最初没有为元素分配存储空间(当 Vec 为空时).
  2. 当元素被添加到向量中时,元素的存储空间是从堆中分配的.Vec 存储指向该存储的指针.
  3. 删除向量时,首先释放元素的存储空间,然后释放 Vec 本身.
  1. A Vec is allocated. No storage is allocated for the elements initially (when the Vec is empty).
  2. As elements are added to the vector, storage for the elements is allocated from the heap. The Vec stores a pointer to that storage.
  3. When the vector is dropped, the storage for the elements is first freed, then the Vec itself is freed.

对于切片:

  1. 静态或动态地为数组或元素向量分配一些存储空间.
  2. 分配并初始化一个切片以引用该存储的部分或全部元素.切片存储指向第一个元素的指针.
  3. 当切片被删除时,元素的存储不会被释放,因为切片不拥有它;只有切片被丢弃.
  4. 如果存储是动态分配的,它最终会被释放.

编辑

一般来说,借用指针(&'a X)、包含借用指针的类型(X<'a>,其中X 是一个 structenum ,其成员是借用的指针)和特征对象/约束(X+'a,其中 Xtrait),当这些类型用作 structenum 的成员时>.

Generally speaking, a lifetime annotation is required on borrowed pointers (&'a X), on types that contain borrowed pointers (X<'a>, where X is a struct or enum that has a member that is a borrowed pointer) and on trait objects/constraints (X+'a, where X is a trait) when those types are used as members of a struct or enum.

let 绑定和as 运算符的右侧,您通常会编写没有生命周期注释的借用指针类型(即只有&X),因为在这种情况下编译器会推断生命周期.

On let bindings and on the right-hand side of the as operator, you usually write borrowed pointer types without a lifetime annotation (i.e. just &X), because the compiler infers the lifetime in that case.

您需要记住的是,在直接或间接处理借用指针时,生命周期注释是必要的.

What you need to remember is that lifetime annotations are necessary when dealing with borrowed pointers, either directly or indirectly.

如果您想了解有关所有权、借用和生命周期的更多信息,我建议您阅读 Rust 指南关于指针的部分 以及 Rust 参考和生命周期指南

If you want to learn more about ownership, borrowing and lifetimes, I suggest you read the Rust Guide's section on pointers as well as the Rust References and Lifetimes Guide

这篇关于为什么结构中的切片需要生命周期,而不需要向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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