1
2
3
4
5
6
7
8
9
10
| use std::thread;
fn main() {
let name: Option<&str> = thread::current().name();
if let Some(name) = name {
println!("Current thread name: {}", name);
} else {
println!("Current thread is unnamed");
}
}
|
上面代码看起来很简单, 获取当前线程的名字, 但结果并不理想
Rust的编译器非常强, 告诉我们 thread::current() 是一个临时变量, 在这个语句结束时会释放
不过 .name()方法返回的是一个 Option<&str>, 返回临时变量的引用会导致悬垂引用, 生命周期超过那个临时变量的生命周期.
Rust可没有Golang中的逃逸分析, 不会自动将引用的生命周期延长
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| --> src\bin\lifetime.rs:4:16
|
4 | let name = thread::current().name();
| ^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
5 | if let Some(name) = name {
| ---- borrow later used here
|
help: consider using a `let` binding to create a longer lived value
|
4 ~ let binding = thread::current();
5 ~ let name = binding.name();
|
For more information about this error, try `rustc --explain E0716`.
|
修复起来很简单, 编译器也告诉了我们如何做, 我哭死😭
1
2
3
4
5
6
7
8
9
10
11
| use std::thread;
fn main() {
let current_thread = thread::current();
let name: Option<&str> = current_thread.name();
if let Some(name) = name {
println!("Current thread name: {}", name);
} else {
println!("Current thread is unnamed");
}
}
|