Lines borrows from the string, so you can't return it from the function which creates the string. You have to collect it into the owned storage, like Vec.
impl CSV {
pub fn read(&self) -> Result<Lines, Box<dyn std::error::Error + 'static>> {
// creates a temporary string
let tmp = std::fs::read_to_string(self.file_address.as_ref())?;
// is looking at the string
let lines = Lines::new(&tmp);
// the string is destroyed
drop(tmp);
// lines is trying to read a string that doesn't exist
Ok(lines)
}
}
Unfortunately you can't return both String and Lines together, since Rust doesn't support self-referential structures. You have to collect lines into Vec<String>.