I'm trying to return a list of dictionaries(coming from a python background) from this rust function in which I read a csv file using polars library. I think the data type I need to use is Vec<Vec> in this case, if not please correct me.
I've written the following function,
fn read_csv_file(path: &str) -> Vec<Vec<AnyValue>> {
let file = File::open(path).expect("could not open file");
let df = CsvReader::new(file)
.infer_schema(None)
.has_header(true)
.finish()
.unwrap();
let df_height = df.height();
// Get all the rows from dataframe
let mut i = 0;
let mut rows = Vec::new();
while i < df_height {
let row = df.get(i).unwrap();
rows.push(row.to_owned());
i += 1;
}
return rows;
}
but when I try to call it,
error[E0515]: cannot return value referencing local variable `df`
--> src/main.rs:50:12
|
40 | let row = df.get(i).unwrap();
| --------- `df` is borrowed here
...
50 | return rows;
| ^^^^ returns a value referencing data owned by the current function
For more information about this error, try `rustc --explain E0515`.
I tried writing .to_owned() to various parts of the function, but no luck :). Stackoverflow usually gives examples related to borrowed values, but I'm not exactly sure what is borrowed here(it says df, but the row shouldn't be a reference to df at this point).
I'm a bit lost and looking for some help understanding what's going on with my function.