简体   繁体   English

我可以避免使用显式生命周期说明符而是使用引用计数(Rc)吗?

[英]Can I avoid using explicit lifetime specifiers and instead use reference counting (Rc)?

I am reading the Rust Book and everything was pretty simple to understand (thanks to the book's authors), until the section about lifetimes .我正在阅读 Rust 书,一切都很容易理解(感谢本书的作者),直到关于生命周期的部分。 I spent all day, reading a lot of articles on lifetimes and still I am very insecure about using them correctly.我花了一整天时间,阅读了很多关于生命周期的文章,但我仍然对正确使用它们非常不安全。

What I do understand, though, is that the concept of explicit lifetime specifiers aims to solve the problem of dangling references.不过,我确实理解的是,显式生命周期说明符的概念旨在解决悬空引用的问题。 I also know that Rust has reference-counting smart pointers ( Rc ) which I believe is the same as shared_ptr in C++, which has the same purpose: to prevent dangling references.我也知道 Rust 具有引用计数智能指针 ( Rc ),我认为它与 C++ 中的shared_ptr相同,其目的相同:防止悬空引用。

Given that those lifetimes are so horrendous to me, and smart pointers are very familiar and comfortable for me (I used them in C++ a lot), can I avoid the lifetimes in favor of smart pointers?鉴于这些生命周期对我来说是如此可怕,而且智能指针对我来说非常熟悉和舒适(我在 C++ 中经常使用它们),我可以避免使用智能指针的生命周期吗? Or are lifetimes an inevitable thing that I'll have to understand and use in Rust code?还是我必须在 Rust 代码中理解和使用生命周期?

are lifetimes an inevitable thing that I'll have to understand and use in Rust code?我必须在 Rust 代码中理解和使用生命周期吗?

In order to read existing Rust code, you probably don't need to understand lifetimes.为了阅读现有的 Rust 代码,您可能不需要了解生命周期。 The borrow-checker understands them so if it compiles then they are correct and you can just review what the code does.借用检查器理解它们,所以如果它编译,那么它们是正确的,您可以查看代码的作用。

I am very insecure about using them correctly.我对正确使用它们非常不安全。

The most important thing to understand about lifetimes annotations is that they do nothing.关于生命周期注释,最重要的一点是它们什么都不做。 Rather, they are a way to express to the compiler the relationship between references.相反,它们是向编译器表达引用之间关系的一种方式。 For example, if an input and output to a function have the same lifetime, that means that the output contains a reference to the input (or part of it) and therefore is not allowed to live longer than the input.例如,如果输入和 output 到 function 具有相同的生命周期,这意味着 output 包含对输入的引用(或输入的一部分),因此不允许对输入进行引用。 Using them "incorrectly" means that you are telling the compiler something about the lifetime of a reference which it can prove to be untrue - and it will give you an error, so there is nothing to be insecure about! “错误地”使用它们意味着你告诉编译器一些关于引用的生命周期的事情,它可以证明是不真实的——它会给你一个错误,所以没有什么不安全的!

can I avoid the lifetimes in favor of smart pointers?我可以避免使用智能指针的生命周期吗?

You could choose to avoid using references altogether and use Rc everywhere.可以选择完全避免使用引用并在任何地方使用Rc You would be missing out on one of the big features of Rust: lifetimes and references form one of the most important zero-cost abstractions, which enable Rust to be fast and safe at the same time.您将错过 Rust 的一大特点:生命周期和引用构成了最重要的零成本抽象之一,它使 Rust 能够同时快速和安全。 There is code written in Rust that nobody would attempt to write in C/C++ because a human could never be absolutely certain that they haven't introduced a memory bug.有代码用 Rust 编写,没有人会尝试用 C/C++ 编写,因为人类永远无法绝对确定他们没有引入 memory 错误。 Avoiding Rust references in favour of smart pointers will mostly result in slower code, because smart pointers have runtime overhead.避免 Rust 引用以支持智能指针将主要导致代码变慢,因为智能指针具有运行时开销。

Many APIs use references.许多 API 使用引用。 In order to use those APIs you will need to have at least some grasp of what is going on.为了使用这些 API,您至少需要对正在发生的事情有所了解。

The best way to understand is just to write code and gain an intuition from what works and what doesn't.最好的理解方式就是编写代码并从哪些有效哪些无效中获得直觉。 Rust's error messages are excellent and will help a lot with forming that intuition. Rust 的错误信息非常好,对形成这种直觉很有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在结构中使用可变引用而不是不可变引用时,为什么会出现生命周期错误? - Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable reference? 我怎样才能让 impl Trait 使用适当的生命周期来对具有另一个生命周期的值进行可变引用? - How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it? 在装箱的将来使用参考变量时,“需要显式寿命” - “explicit lifetime required” when using a reference variable in a boxed future 如何使用生命周期来解决“引用必须对静态生命周期有效” - How can I use lifetime bounds to solve “reference must be valid for the static lifetime” 我可以通过持有引用来避免克隆字符串吗? - Can I avoid cloning Strings by holding a reference instead? 我是否正确使用继承,借用指针和显式生命周期注释? - Am I using inheritance, borrowed pointers, and explicit lifetime annotations correctly? 为什么我可以使用可变变量使其生命周期与不可变引用重叠,但我不能以相同的方式使用可变引用? - Why can I use a mutable variable such that its lifetime overlaps with an immutable reference, but I can't use a mutable reference in the same way? SFML中的生命周期说明符和泛型 - Lifetime specifiers and generics in SFML 使用具有显式生命周期的特征时,无法推断借位表达式的生命周期 - Unable to infer lifetime for borrow expression when using a trait with an explicit lifetime 我应该尽可能避免在 Rust 中使用 Rc 和 RefCell 吗? - Should I avoid to use Rc and RefCell when possible in Rust?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM