Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix `returns a value referencing data owned by the current function` when using std::str::Chars in Rust

I have a function tk_from_file that reads a file, and returns a struct Tokenizer, with a Peekable<Chars<'a>> over the contents of that file.

The variable content is from the Result<String> returned from std::fs::read_to_string

However, when I try to create the struct and return it, I get the following error (E0515):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

30 |           return Tokenizer {
   |  ________________^
31 | |             content: content.clone().chars().peekable(),
   | |                      --------------- temporary value created here
32 | |             loc: FileLocation {
33 | |                 file: path,
...  |
36 | |             }
37 | |         };
   | |_________^ returns a value referencing data owned by the current function

I have tried using it with clone() and without, as well as clone() on the Chars iterator in addition to the String, but it still doesn’t work.

>Solution :

It doesn’t matter if you clone this String or not. String created in this function is owned by this function, therefore you cannot return any references to it from the function, because when the stack frame of the function is popped the String will be dealocated and you will have a dangling pointer, which is a big no no in rust world.

If you want to reference this String created when reading file you must return owned String from the function somehow and give an ownership of it to the caller. One solution would be to store a String inside Tokenizer struct and provide a method on it that would borrow a String and return iterator.

struct Tokenizer {
    content: String,
    ...
}

impl Tokenizer {
    fn content(&self) -> Peekable<Chars<'_>> {
        self.content.chars().peekable()
    }
}

With following structure you can pass ownership of file content to the caller and allow them to borrow it for as long as they want. You can read more about ownership and borrowing in this chapter of The Book.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading