生锈寿命的概念

发布于 2025-01-25 02:07:01 字数 1111 浏览 4 评论 0原文

问题

我通常使用C ++ lang,最近我正在学习Rust Lang,但现在使终生的概念感到困惑。
我对一生的理解如下。这是正确的吗?

  • 生命周期是实例的属性。
  • 生命周期代表实例的有效范围。

以上问题的背景

以下代码是在这里<这里< /a>。

    {
        let r;                // ---------+-- 'a
                              //          |
        {                     //          |
            let x = 5;        // -+-- 'b  |
            r = &x;           //  |       |
        }                     // -+       |
                              //          |
        println!("r: {}", r); //          |
    }                         // ---------+

文档'B也是一生。
但是,如果我的理解是正确的,“ a不是终身,只是符号r的范围...是``真的是一生吗?

PS

有两件事称为“生命周期”:Value的寿命,以及附加到参考的寿命。

谢谢你!也许我比以前更了解...

Question

I usually use c++ lang, and recently I'm learning rust lang but now confusing the concept of lifetime.
My understanding for lifetime is as follows. Is this correct?

  • Lifetime is an attribute of instance.
  • Lifetime represents the valid scope of instance.

Background of above question

The following code is a sample code at here.

    {
        let r;                // ---------+-- 'a
                              //          |
        {                     //          |
            let x = 5;        // -+-- 'b  |
            r = &x;           //  |       |
        }                     // -+       |
                              //          |
        println!("r: {}", r); //          |
    }                         // ---------+

The document said that 'a is a lifetime, and 'b is also a lifetime.
but if my understanding is correct, 'a is not lifetime, just scope of symbol r... Is `a really lifetime?

P.S.

There are two things named "a lifetime": value's lifetime, and the lifetime attached to a reference.

Thank you! Maybe I understood a little bit more than before...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-02-01 02:07:01

生命周期并不是实例的属性,而是相反的方式。借用实例(获取引用)时,将其借用特定的生命周期。借用检查员将尝试最大程度地减少借用的寿命,以便在确保代码仍然安全的同时尽可能宽容。

一生是您传达您需要参考持续时间多长时间的方式,并且鉴于该信息,编译器将检查没有任何违反该规则(通过持有更长的参考),并且至少可以使用该参考文献只要您需要它。可以以编译器可以检查类型的方式来考虑这一点,以确保您不会将浮子分配给整数。

终生也独立于范围。 Rust现在有非静物寿命( https://github.com/rust-lang/rust-lang/ rfcs/pull/2094 - 参见什么是非律中的寿命?有关更详细的解释),这意味着在给定的范围内,借用检查器能够确定借口比包含范围较短。

A lifetime is not so much an attribute of an instance, it's more the opposite way around; when an instance is borrowed (a reference is taken), it is borrowed for a specific lifetime. The borrow checker will try to minimise the lifetime of the borrow so that it can be as permissive as possible whilst ensuring code is still safe.

Lifetimes are your way of communicating how long you need a reference to last to the compiler, and given that information, the compiler will check that nothing violates that rule (by holding the reference for longer), and that the reference is available for at least as long as you require it. This can be thought of in much the same way that a compiler can check type to ensure you don't assign a float to an integer.

Lifetimes are also independent of scope. Rust now has non-lexical lifetimes (https://github.com/rust-lang/rfcs/pull/2094 — see What are non-lexical lifetimes? for more detailed explanation), meaning that within a given scope, the borrow checker is capable of determining that borrows have shorter lifetimes than the containing scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文