You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The method from_file() for Writer from BurntSushi's CSV crate has been deprecated a while ago and replaced by from_path(). As a relatively new person to Rust, this kind of got me stuck with the presented examples for writing into a CSV file until I dug into the documentation :).
Instead of:
let dollar_films = vec![
("A Fistful of Dollars", "Rojo", 1964),
("For a Few Dollars More", "El Indio", 1965),
("The Good, the Bad and the Ugly", "Tuco", 1966),
];
let path = "westerns.csv";
let mut writer = Writer::from_file(path).unwrap();
for row in dollar_films {
writer.encode(row).expect("CSV writer error");
}
The following works just fine using the current version of CSV crate (1.1.1):
let dollar_films = vec![
["A Fistful of Dollars", "Rojo", "1964"],
["For a Few Dollars More", "El Indio", "1965"],
["The Good, the Bad and the Ugly", "Tuco", "1966"],
];
let path = "westerns.cvs";
let mut writer = Writer::from_path(path).unwrap();
for row in dollar_films {
writer.write_record(&row).expect("CSV writer error");
writer.flush();
}
The text was updated successfully, but these errors were encountered:
For others being stuck here, as of CSV v 1.1.3 writer.encode(row)
has changed to writer.serialize(row)
as well.
Then you can keep the type of dollay_films a Vec<(&str, &str, i32)> instead of changing it to Vec<[&str; _]> by storing the years as strings instead of numbers.
Also, to be able to serialize Movie with csv:
in Cargo.toml, add dependency serde = { version = "1.0", features = ["derive"] }
and then in day3.rs
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)] // Debug to be able to print movies
struct Movie { // ..
To skip the csv headers one now needs to configure the reader via:
let mut rb = ReaderBuilder::new();
rb.has_headers(false);
let mut reader = rb.from_path(path).unwrap();
The method
from_file()
for Writer from BurntSushi's CSV crate has been deprecated a while ago and replaced byfrom_path()
. As a relatively new person to Rust, this kind of got me stuck with the presented examples for writing into a CSV file until I dug into the documentation :).Instead of:
The following works just fine using the current version of CSV crate (1.1.1):
The text was updated successfully, but these errors were encountered: