tables.rs
577 B · rust · 21 lines
1use std::fs;2use std::path::Path;34pub fn write_csv(path: &Path, header: &[String], rows: &[Vec<String>]) {5 let mut out = header.join(",");6 out.push('\n');7 for row in rows {8 let fields: Vec<String> = row.iter().map(|field| quote(field)).collect();9 out.push_str(&fields.join(","));10 out.push('\n');11 }12 fs::write(path, out).expect("the table is writable");13}1415fn quote(field: &str) -> String {16 if field.contains([',', '"', '\n']) {17 format!("\"{}\"", field.replace('"', "\"\""))18 } else {19 field.to_string()20 }21}