I'm a Rust beginner and am working on a small personal project. I'm working on a function that takes a Vec of records and turns them into strings to be used as rows in a CSV file.
My currently incomplete function looks like
pub fn write_csv_records<T: CSVWritable>( file_path: &str, seperator: char, records: Vec<Box<dyn CSVWritable>>, columns: Vec<String> ) -> ()
{
let body = records.iter()
.map(|r| r.to_csv_row() )
.map(|row_values| {
columns.iter()
// this is my problem the closure in unwrap_or_else causes in issue!
.map( |column| row_values.get( column ).unwrap_or_else(|| &String::from("") ).clone() )
.collect::<Vec<String>>()
.join( &seperator.to_string() )
})
.collect::<Vec<String>>()
.join( "\n" );
}
I have had a persistent error that reads "cannot return a reference to data owned by the current function".
I'm at a loss for how to proceed. Is this related to lifetimes? Am I missing how unwrap_or_else works? What is the right way to provide a default value when something is absent in the row_values HashMap?