I've the below code that is reading from csv
file, and saving the data in Vec
, reading is done correctly, but panic while trying saving it in the vec INs
:
use std::error::Error;
use std::io;
use std::process;
use std::path::Path;
#[macro_use] extern crate serde_derive;
#[derive(Clone, Debug, Deserialize)]
struct INs<'a> {
item: &'a str,
date: &'a str,
batch: &'a str,
quantity: u64,
}
fn main() {
let mut ins = Inventory::new();
let file_path_ins = Path::new("src/ins.csv");
let mut rdr_ins = csv::ReaderBuilder::new()
.has_headers(true)
.delimiter(b',')
.from_path(file_path_ins).unwrap();
for result in rdr_ins.records() {
let record: INs = result.unwrap().deserialize(None).unwrap();
ins.push(INs { item: record.item, date: record.date,
batch: record.batch, quantity: record.quantity, });
};
// Tried also:
/* for result in rdr_ins.records() {
match result.unwrap().deserialize::<INs>(None){
Ok(record) => {
println!("{:?}", record); // This alone is ok, with the lines under it panic
// ins.push(INs { item: record.item, date: record.date,
// batch: record.batch, quantity: record.quantity, });
},
Err(e) => {
println!("Error: {:?}", e);
},
}
}
*/
}
I got the below error, and do not understand where the compiler want me to use let
as shown below:
error[E0716]: temporary value dropped while borrowed
--> src\main.rs:100:28
|
100 | let record: INs = result.unwrap().deserialize(None).unwrap();
| ^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
101 | ins.push(INs { item: record.item, date: record.date,
| ----------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value