gpt4 book ai didi

rust - str 类型的生命周期/借用问题

转载 作者:行者123 更新时间:2023-12-03 11:24:28 25 4
gpt4 key购买 nike

为什么这段代码会编译?

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}

fn main() {
let x = "eee";
let &m;
{
let y = "tttt";
m = longest(&x, &y);
}
println!("ahahah: {}", m);
}

对我来说,由于生命周期,应该存在编译错误。
如果我用 i64 写同样的代码,我收到一个错误。
fn ooo<'a>(x: &'a i64, y: &'a i64) -> &'a i64 {
if x > y {
x
} else {
y
}
}

fn main() {
let x = 3;
let &m;
{
let y = 5;
m = ooo(&x, &y);
}
println!("ahahah: {}", m);
}

错误是:

error[E0597]: `y` does not live long enough
--> src/main.rs:103:25
|
103 | m = ooo(&x, &y);
| ^^ borrowed value does not live long enough
104 | }
| - `y` dropped here while still borrowed
105 | println!("ahahah: {}", m);
| - borrow later used here

最佳答案

为了理解这一点,我们需要知道一些事情。第一个是字符串文字的类型。任何字符串文字(如 "foo" )都具有 &'static str 类型。这是对字符串切片的引用,而且,它是一个静态引用。这种引用持续整个程序长度,并且可以根据需要强制到任何其他生命周期。

这意味着在您的第一段代码中, x 和 y 已经都是引用并且具有 &'static str 类型。调用 longest(&x, &y) 仍然有效的原因(即使 &x&y 的类型为 &&'static str )是由于 Deref coercion. longest(&x, &y) 确实被脱糖为 longest(&*x, &*y) 以使类型匹配。

让我们分析第一段代码中的生命周期。

fn main() {
// x: &'static str
let x = "eee";
// Using let patterns in a forward declaration doesn't really make sense
// It's used for things like
// let (x, y) = fn_that_returns_tuple();
// or
// let &x = fn_that_returns_reference();
// Here, it's the same as just `let m;`.
let &m;
{
// y: &'static str
let y = "tttt";
// This is the same as `longest(x, y)` due to autoderef
// m: &'static str
m = longest(&x, &y);
}
// `m: &static str`, so it's still valid here
println!("ahahah: {}", m);
}

(playground)

使用 let &m; 您可能意味着类似 let m: &str 的东西来强制其类型。我认为这实际上可以确保 m 中引用的生命周期从该前向声明开始。但是由于 m 无论如何都具有 &'static str 类型,所以没关系。

现在让我们看看带有 i64 的第二个版本。
fn main() {
// x: i64
// This is a local variable
// and will be dropped at the end of `main`.
let x = 3;
// Again, this doesn't really make sense.
let &m;
// If we change it to `let m: &i64`, the error changes,
// which I'll discuss below.
{
// Call the lifetime roughly corresponding to this block `'block`.
// y: i64
// This is a local variable,
// and will be dropped at the end of the block.
let y = 5;
// Since `y` is local, the lifetime of the reference here
// can't be longer than this block.
// &y: &'block i64
// m: &'block i64
m = ooo(&x, &y);
} // Now the lifetime `'block` is over.
// So `m` has a lifetime that's over
// so we get an error here.
println!("ahahah: {}", m);
}

(playground)

如果我们将 m 的声明更改为 let m: &i64(这就是我认为你的意思),错误就会改变。

error[E0597]: `y` does not live long enough
--> src/main.rs:26:21
|
26 | m = ooo(&x, &y);
| ^^ borrowed value does not live long enough
27 | } // Now the lifetime `'block` is over.
| - `y` dropped here while still borrowed
...
30 | println!("ahahah: {}", m);
| - borrow later used here

(playground)

所以现在我们明确希望 m 与外部块一样长,但我们不能让 y 持续那么长时间,所以错误发生在调用 ooo 时。

由于这两个程序都在处理文字,我们实际上可以编译第二个版本。为此,我们必须利用静态推广。可以在 Rust 1.21 announcement (这是引入此版本的版本)或 this question 中找到关于这意味着什么的一个很好的总结。

简而言之,如果我们直接引用一个字面值,该引用可能会被提升为静态引用。也就是说,它不再引用局部变量。
fn ooo<'a>(x: &'a i64, y: &'a i64) -> &'a i64 {
if x > y {
x
} else {
y
}
}

fn main() {
// due to promotion
// x: &'static i64
let x = &3;
let m;
{
// due to promotion
// y: &'static i64
let y = &5;
// m: &'static i64
m = ooo(x, y);
}
// So `m`'s lifetime is still active
println!("ahahah: {}", m);
}

(playground)

关于rust - str 类型的生命周期/借用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61538639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com