Passing a borrowed CSV writer as argument to a function


I tried to pass a borrowed CSV writer to a function.
I search and, I found a similar topic here, but in this case he doesn't pass a borrowed writer.

I try to do this :

use std::io;

fn write_record<W: io::Write>(wtr: &mut csv::Writer<W>) {

fn main() {
    let mut wtr = csv::Writer::from_path("./toto.csv").unwrap();
    for _i in 1..5 {

But I have a “mismatched types” on the “write_record(&wtr)" line.

error[E0308]: mismatched types
  --> src/
10 |         write_record(&wtr);
   |         ------------ ^^^^ types differ in mutability
   |         |
   |         arguments to this function are incorrect
   = note: expected mutable reference `&mut Writer<_>`
                      found reference `&Writer<File>`

Why this is not working ? And how can I fix it ?

write_record(&wtr) just passes a non-mutable reference as an argument. You need to explicitly indicate that you want a mutable reference by writing write_record(&mut wtr).

Thank you !

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.