简体   繁体   English

为什么编译器假定返回的引用与结构具有相同的生命周期?

[英]Why does the compiler assume the returned reference has the same lifetime as the struct?

I have the following Rust code:我有以下 Rust 代码:

pub struct Foo {}

impl Foo {
    fn bar(&self) -> &[u8] {
        // obtain some pointer from FFI and return it as a slice (ignore the zeros)
        unsafe { std::slice::from_raw_parts(0 as *const u8, 0) }
    }
}

fn main() {
    let a;
    {
        let b = Foo {};
        a = b.bar();
    }
    a;
}

When compiling, the following error is produced:编译时,会产生以下错误:

error[E0597]: `b` does not live long enough
  --> src/main.rs:14:13
   |
14 |         a = b.bar();
   |             ^ borrowed value does not live long enough
15 |     }
   |     - `b` dropped here while still borrowed
16 |     a;
   |     - borrow later used here

This is the outcome I would expect;这是我所期望的结果; The compiler won't let a reference to some object (from FFI in my case) outlive its container.编译器不会让对某些 object(在我的例子中来自 FFI)的引用超过其容器。 However, I am confused as to why this is.但是,我很困惑为什么会这样。 Why does the compiler assume that the returned reference should have the same lifetime as the struct?为什么编译器假定返回的引用应该与结构具有相同的生命周期? For all it knows, the returned reference could be freed at any time.据它所知,返回的引用可以随时释放。

Due to lifetime elision , you don't have to annotate every reference with lifetimes if the compiler can figure it out.由于生命周期省略,如果编译器可以弄清楚,您不必用生命周期注释每个引用。 However, you should know it exists so that you know when you write:但是,您应该知道它的存在,以便您在编写时知道:

fn bar(&self) -> &[u8]

The compiler deduces it as:编译器将其推断为:

fn bar<'a>(&'a self) -> &'a [u8]

So the slice from b.bar() is bound to lifetime of b .所以b.bar()的切片绑定到b的生命周期。

暂无
暂无

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

相关问题 为什么编译器假定if let的值应为`()`? - Why does the compiler assume that the value of if let should be `()`? 为什么在我引用的结构上未使用生命周期? - Why is a lifetime unused on a struct that I have a reference to? 给出生命周期的编译器在结构中询问生命周期 - Compiler asking for lifetime in struct when lifetime is given 在嵌套范围内覆盖结构字段(与其他字段具有相同的生命周期)时出现“X 寿命不够长”错误 - "X does not live long enough" error when overriding struct field (that has the same lifetime as other fields) inside nested scope 具有可变引用的递归结构中的生命周期 - Lifetime in recursive struct with mutable reference 参考的寿命比同一范围的值短吗? - Reference has shorter lifetime than its value from same scope? 是否有可能包含一个引用,该引用的值的寿命比该结构的寿命短? - Is it possible to have a struct which contains a reference to a value which has a shorter lifetime than the struct? 为什么这个 Rust 代码编译时在结构上有生命周期绑定,但如果绑定仅在 impl 上,则会给出生命周期错误? - Why does this Rust code compile with a lifetime bound on the struct, but give a lifetime error if the bound is only on the impl? 告诉 Rust 编译器参数的生命周期始终与结构的生命周期相同 - Tell the Rust compiler that the lifetime of a parameter is always identical to a struct's lifetime 当我在结构中使用可变引用而不是不可变引用时,为什么会出现生命周期错误? - Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM