Expand description
This library provides a lightweight way to export data to the CSV format via trait implementation.
[dependencies]
to_csv = "1.0"
§Example
struct TypeA {
name: String,
value: String,
date: String,
}
impl CSV for TypeA {
fn headers(&self) -> String {
format!("{},{},{}", "Data Name", "Amount", "Date",)
}
fn row(&self) -> String {
format!(
"{},{},{}",
self.name,
self.value,
self.date,
)
}
}
fn main() {
let entries = vec![TypeA {name: "Test Data".to_string(), value: "10".to_string(), date: "6/3/2025".to_string()}]
let _ = to_csv_file("csv_file.csv", &entries);
}
§Result
Data Name,Amount,Date,
Test Data,10,6/3/2025,
Traits§
- CSV
- Implement this trait on the structs you will be passing to this libs functions.
Functions§
- to_
csv_ as_ file - Converts given Vec
that implements the CSV trait to a csv string then saves it to the given file_path - to_
csv_ as_ string - Converts given Vec
that implements the CSV trait to a csv string. - to_
csv_ as_ string_ with_ encode - Converts given Vec
that implements the CSV trait to a csv string that is URL encoded