了解Rust中的参考寿命 [英] Understanding of reference life time in Rust

查看:48
本文介绍了了解Rust中的参考寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rust的新用户,正在读一本书完整的Rust编程参考指南.书中有一个例子:

I'm a new Rust user and I'm reading a book The Complete Rust Programming Reference Guide. In the book there is an example:

fn main() {
    let mut a = String::from("testing");
    let a_ref = &mut a;
    a_ref.push('!');

    println!("{}", a);
}

这本书说代码会产生错误.

The book states the code will generate an error.

但是,我可以在本地计算机上运行它而没有任何问题.这是因为我使用的是更新的Rust编译器[ Rustc 1.41.0-nightly(412f43ac5 2019-11-24)]并且该代码在较旧的版本上不起作用吗?我已经阅读了Rust官方书中的某些章节.据我了解,引用 a_ref 的生​​存期在其最后一次使用时结束,即 a_ref.push('!'); .之后, a_ref 消失了,并且 a 应该可以正常使用了.我的理解正确吗?

However, on my local machine, I can run it without any issue. Is this because I'm using a newer Rust compiler [rustc 1.41.0-nightly (412f43ac5 2019-11-24)] and the code doesn't work on older ones? I've read some chapters of the official Rust book. As I understand, the lifetime of the reference a_ref ends at its last usage, which is a_ref.push('!');. After that a_ref is gone and a should be able to be used without issue. Is my understanding correct?

推荐答案

最有可能发生的情况是,您正在阅读的书是在教导生命,而不考虑非词汇生命.这是有道理的;词汇寿命是最容易理解的.

The most likely occurrence is that the book you're reading is teaching lifetimes disregarding non-lexical-lifetimes. This would make sense; lexical lifetimes are the most straightforward to understand.

运行以下命令将还原到非词汇生命出现之前的时间:

Running the following will revert to prior to when non-lexical-lifetimes came around:

rustup default 1.30

这会将rustc恢复到版本 1.31 之前的版本,根据

This will revert rustc to prior to version 1.31, which according to this document is the minimum version for nll.

运行此命令将导致完全相同的错误,如下所示:

Running this results in the exact same error as shown:

> cargo run
   Compiling forum_examples v0.1.0 (C:\Users\user\Desktop\forum_examples)
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
 --> src\main.rs:6:20
  |
3 |     let a_ref = &mut a;
  |                      - mutable borrow occurs here
...
6 |     println!("{}", a);
  |                    ^ immutable borrow occurs here
7 | }
  | - mutable borrow ends here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0502`.
error: Could not compile `forum_examples`.

To learn more, run the command again with --verbose.

您可以选择使用此版本的编译器(或2015版的1.35版)来遵循本书,或者您可以使用此经验法则来确定为什么不按照本书进行编译,但确实如此现在有编译器存在:如果编译器会发现不再需要引用,则会删除该引用.在您的示例中,编译器看到之后不再需要 a_ref ,因此它会在此之后插入一个隐式drop.请注意,这仅适用于引用,不适用于警戒,也不适用于涉及生命周期的更复杂的类型(特别是不能调用 drop 代码的任何事物).

You may choose to use this version of the compiler (Or version 1.35 with 2015 edition) to follow the book to the letter or you may use this rule of thumb to determine why it doesn't compile according to the book but does with the compiler present today: The compiler will drop a reference if it should see that it is no longer needed. In your example, the compiler sees that a_ref is no longer needed afterwards, so it's inserting an implicit drop right after for that. Note that this only works for references, and not for guards, or more complex types involving lifetimes (Especially not anything that could invoke drop code).

这篇关于了解Rust中的参考寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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