#Is there a more straightforward way to get the lines of a file as &str?

1 messages · Page 1 of 1 (latest)

hollow nova
#

So far I have only been able to accomplish this with these two lines:

    let lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
    let lines: Vec<&str> = lines.iter().map(|s| s.as_str()).collect();

If I try to do the as_str() in the first line, I get a compiler error that I'm trying to return a value referencing a temporary value even if I clone after unwraping.

tawny totem
#

The &strs in lines will reference contents, so contents cannot be moved or accessed mutably until you're done with lines.