Running the below code:
extern crate csv; // csv = "1.0.0-beta.5"
use std::fs::File;
fn main() {
let file_path = "file.csv";
let file = File::open(file_path).unwrap();
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_reader(file);
let headers = rdr.headers().unwrap();
println!("{:?}", headers);
//println!("{:?}", rdr.headers().unwrap());
for result in rdr.records() {
let record = result.unwrap();
println!("{:?}", record);
}
}
Results in:
error[E0499]: cannot borrow `rdr` as mutable more than once at a time
--> src/main.rs:17:19
|
12 | let headers = rdr.headers().unwrap();
| --- first mutable borrow occurs here
...
17 | for result in rdr.records() {
| ^^^ second mutable borrow occurs here
...
21 | }
| - first borrow ends here
error: aborting due to previous error
If I change these lines:
let headers = rdr.headers().unwrap();
println!("{:?}", headers);
//println!("{:?}", rdr.headers().unwrap());
to the below the code works:
//let headers = rdr.headers().unwrap();
//println!("{:?}", headers);
println!("{:?}", rdr.headers().unwrap());
Similarly if I clone
the headers it also works:
let headers = rdr.headers().unwrap().clone();
println!("{:?}", headers);
I read a related question however that question was about a function that the user wrote themselves whereas this use case is where a function was provided by a library.
How should such errors be dealt with and what exactly is happening to cause this error?