Type conversion and passing to CSV Writer raises an error

A newbie question: what error is happening here? And how to work around it?

Here's the sketch that represents my problem with csv::Writer. Replacing &[...] with vec![...] does not change the error.

extern crate csv;
use std::error::Error;
use csv::Writer;

fn main() -> Result<(), Box<dyn Error>>{
	let mut wr = Writer::from_path("/tmp/mycsv.csv")?;
	let id: i64 = 1231313122;
	let lon: f64 = 12.4567789;
	let lat: f64 = 34.567890;
	wr.write_record(&["id", "lon", "lat"])?;
	wr.write_record(&[id as f64, lon, lat])?;
	              //^^^^^^^^^^^^^^^^^^^^^^ the trait `AsRef<[u8]>` is not implemented for `f64`
	Ok(())
}

The write_record method requires string-like values. You'll need to convert your floats into strings. Alternatively, you may also be able to use the serialize method.

That worked, thanks!

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.