延长字符串切片的借用生命周期 [英] Extending borrowed lifetime for String slice
问题描述
我有一个读取文件的函数,并为每一行将它添加到 &str
类型的 HashSet
,但我不知道如何告诉借用检查器增加生命周期.
这是我目前的功能:
fn build_collection_set(reader: &mut BufReader) ->哈希集<&str>{let mut collection_set: HashSet<&str>= HashSet::new();对于 reader.lines() { 中的行让 line = line.unwrap();如果 line.len() >0 {collection_set.insert(&*line);}}返回集合集;}
如何让 Rust 知道我想让它保持更长时间?
但我不知道如何告诉借用检查器增加生命周期.
这不可能.
<小时>在 C、C++ 或 Rust 中,值的生命周期定义为:
- 根据它的词法范围,如果它绑定到一个自动变量
- 根据它的动态范围,如果它是在堆上分配的
您可以创建引用此值的变量,如果您的引用存在时间比该值长,那么您就有了一个悬空引用:
- 在 C 和 C++ 中,最好什么都不做
- 在 Rust 中,编译器将拒绝编译您的代码
为了验证您的程序,Rust 编译器将要求您注释引用的生命周期;您将在 &'a T
中使用诸如 'a
之类的生命周期注释,它允许命名生命周期以记录多个值的生命周期之间的关系.
这里的操作词是文档:生命周期是无形的,不能被影响,生命周期注释'a
只是一个允许引用它的名称.><小时>
所以?
每当您发现自己想要延长引用的生命周期时,您应该考虑的是延长引用的生命周期……或者干脆不使用引用而是使用值.
在这种情况下,一个简单的解决方案是返回 String
而不是 &str
:
fn build_collection_set(reader: &mut BufReader) ->哈希集<字符串>{让 mut collection_set = HashSet::new();对于 reader.lines() { 中的行让 line = line.unwrap();如果 line.len() >0 {collection_set.insert(行);}}收藏集}
I have a function that reads in a file, and for each line adds it to a HashSet
of type &str
, but I can't work out how to tell the borrow checker to increase the lifetime.
Here's my function so far:
fn build_collection_set(reader: &mut BufReader<File>) -> HashSet<&str> {
let mut collection_set: HashSet<&str> = HashSet::new();
for line in reader.lines() {
let line = line.unwrap();
if line.len() > 0 {
collection_set.insert(&*line);
}
}
return collection_set;
}
How do I let Rust know I want to keep it around longer?
but I can't work out how to tell the borrow checker to increase the lifetime.
It's impossible.
The lifetime of a value, in C, C++ or Rust, is defined either:
- by its lexical scope, if it is bound to an automatic variable
- by its dynamic scope, if it is allocated on the heap
You can create variables which reference this value, and if your reference lives longer than the value, then you have a dangling reference:
- in C and C++, you better do nothing with it
- in Rust, the compiler will refuse to compile your code
In order to validate your program, the Rust compiler will require that you annotate the lifetime of your references; you will use lifetime annotations such as 'a
in &'a T
which allow naming a lifetime in order to document the relationship between the lifetime of multiple values.
The operative word is document here: a lifetime is intangible and cannot be influenced, the lifetime annotation 'a
is just a name to allow referring to it.
So?
Whenever you find yourself wanting to extend the lifetime of a reference, what you should be looking at instead is extending the lifetime of the referred... or simply not use a reference but a value instead.
In this case, a simple solution is to return String
instead of &str
:
fn build_collection_set(reader: &mut BufReader<File>) -> HashSet<String> {
let mut collection_set = HashSet::new();
for line in reader.lines() {
let line = line.unwrap();
if line.len() > 0 {
collection_set.insert(line);
}
}
collection_set
}
这篇关于延长字符串切片的借用生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!