pub struct Writer<W: Write> { /* private fields */ }Expand description
Writes CSV data to a std::io::Write sink.
The writer handles auto-quoting of fields that contain the delimiter, newlines, or double-quotes. It also validates that all rows have the same number of fields (unless flexible mode is enabled).
Internal buffering is used to avoid many small writes to the
underlying writer. Callers should not wrap the writer in a
BufWriter.
§Example
let mut w = Writer::new(Vec::new());
w.write_row(["name", "age", "city"]).unwrap();
w.write_row(["Alice", "30", "New York, NY"]).unwrap();
let result = String::from_utf8(w.into_inner().unwrap()).unwrap();
assert_eq!(result, "name,age,city\r\nAlice,30,\"New York, NY\"\r\n");Implementations§
Source§impl<W: Write> Writer<W>
impl<W: Write> Writer<W>
Sourcepub fn delimiter(&mut self, byte: u8) -> &mut Self
pub fn delimiter(&mut self, byte: u8) -> &mut Self
Set the field delimiter byte (default is ,).
Sourcepub fn flexible(&mut self, yes: bool) -> &mut Self
pub fn flexible(&mut self, yes: bool) -> &mut Self
Enable flexible mode, which skips the field count consistency check.
Sourcepub fn write_row<I, T>(&mut self, row: I) -> Result<(), WriteError>
pub fn write_row<I, T>(&mut self, row: I) -> Result<(), WriteError>
Write a single row. Each element in the iterator is written as a
CSV field. Fields are auto-quoted if they contain the delimiter,
a newline (\n or \r), or a double-quote (").
Whitespace trimming is never performed. The empty byte slice &[]
produces an empty field.
§Errors
Returns WriteError::InconsistentFieldCount if the number of
fields differs from previous rows (unless flexible mode is enabled).
Returns WriteError::Io if the underlying writer fails.
Sourcepub fn flush(&mut self) -> Result<(), WriteError>
pub fn flush(&mut self) -> Result<(), WriteError>
Flush the internal buffer to the underlying writer.
Sourcepub fn into_inner(self) -> Result<W, WriteError>
pub fn into_inner(self) -> Result<W, WriteError>
Unwrap the writer, flushing any remaining data and returning the underlying writer.