Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

rust - Extending borrowed lifetime for String slice

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...