1

For the following code:

fn get_lines() -> String {
    String::from("Hello\nWorld")
}

fn get_first_line(s: &String) -> &str {
    s.lines().next().unwrap()
}

struct World<'a> {
    a_str: &'a str,
}

fn work<'a>() -> World<'a> {
    let s1 = get_lines();
    let s2 = get_first_line(&s1);

    World { a_str: s2 }
}

fn main() {
    let w = work();
}

I got the following error:

error[E0515]: cannot return value referencing local variable `s1`
  --> src/main.rs:17:5
   |
15 |     let s2 = get_first_line(&s1);
   |                             --- `s1` is borrowed here
16 | 
17 |     World { a_str: s2 }
   |     ^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function

How to build a struct instance using s2? Is it a conceptual error of the World struct?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
bux
  • 7,087
  • 11
  • 45
  • 86
  • "How to build a Struct instance using s2 ?" => `s2.to_string()`. "Is it a conception error of World struct ?" => why not put use a `String` in `World` ? – Stargateur Apr 06 '19 at 11:34
  • @Stargateur a String is a possibility yes. I am a beginner so i have no arguments ^^ – bux Apr 06 '19 at 11:49
  • 1
    Also see [Return local String as a slice (&str)](https://stackoverflow.com/q/29428227/3650362), [Why can't I return an &str value generated from a String?](https://stackoverflow.com/q/29781331/3650362), [Is there any way to return a reference to a variable created in a function?](https://stackoverflow.com/q/32682876/3650362)... Honestly, if you just search [rust] for [cannot return value referencing local variable](https://stackoverflow.com/search?q=%5Brust%5D+cannot+return+value+referencing+local+variable) you will find a lot of related questions with answers. – trent Apr 06 '19 at 12:05

1 Answers1

1

World refers to a str slice which must be owned by something else. Your function work allocates a new String (through get_lines), and makes a reference into it (through get_first_line). When it returns, the String goes out of scope and will be dropped, so you cannot keep a reference to it, since the thing it refers to is no longer there.

If you want a World object that does not depend on a String owned by something else, it will need to own the data itself: Contain a String instead of a &'a str.

See also 'dangling references' in the Rust Book.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mara Bos
  • 955
  • 5
  • 11
  • Please refrain from answering [questions that are duplicates of existing questions](https://stackoverflow.com/questions/55548553/struct-str-attribute-must-be-reference#comment97799445_55548553). Doing so spreads little bits of knowledge around to many places as opposed to consolidating them in a few well-maintained locations. It also makes it less likely for previous answers to get deserved upvotes and critical eyes. In the worst cases, it encourages question askers to not search for duplicates themselves. – Shepmaster Apr 06 '19 at 19:36