packages

csv::write

On this page

Writer: records out, each field quoted only as the dialect's QuoteStyle says, to a stream or to memory.

Structs

WriterSource

pub struct Writer

Writes records.

nabi
let w = csv::Dialect::new().to_buffer();
try! w.write_record(["name", "note"]);
try! w.write_record(["Ann", "says \"hi\", twice"]);
assert(w.as_str() == "name,note\nAnn,\"says \"\"hi\"\", twice\"\n");
  • pub fn write_field(self, s: str) throws (CsvError)§

    Source

    One field of the current record.

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write_field("a");
    try! w.write_field("b c");
    try! w.end_record();
    assert(w.as_str() == "a,b c\n");
    
  • pub fn write_quoted(self, s: str) throws (CsvError)§

    Source

    One field, quoted whatever the style (so a field that reads as a null marker, such as NA, stays a value).

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write_quoted("NA");
    try! w.write_field("NA");
    try! w.end_record();
    assert(w.as_str() == "\"NA\",NA\n");
    
  • pub fn write_unquoted(self, s: str) throws (CsvError)§

    Source

    One field as it is, never quoted; a field that needs quotes is an error. An empty one alone makes a blank line.

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write_unquoted("");
    try! w.end_record();
    assert(w.as_str() == "\n");
    
  • pub fn write_raw(self, s: str) throws (CsvError)§

    Source

    One field exactly as given, with no quoting and no check: for text known to need no quotes, such as a number, at the speed of a copy. Text that needs quotes makes output that reads back wrong.

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write_raw("42");
    try! w.write_raw("2.5");
    try! w.end_record();
    assert(w.as_str() == "42,2.5\n");
    
  • pub fn end_record(self) throws (CsvError)§

    Source

    End the current record.

    nabi
    let w = csv::Dialect::new().crlf(true).to_buffer();
    try! w.write_field("x");
    try! w.end_record();
    assert(w.as_str() == "x\r\n");
    
  • pub fn write_record(self, fields: List<str>) throws (CsvError)§

    Source

    A whole record.

    nabi
    let w = csv::Dialect::tsv().to_buffer();
    try! w.write_record(["a", "b"]);
    assert(w.as_str() == "a\tb\n");
    
  • pub fn write(self, r: Record) throws (CsvError)§

    Source

    A record read by a Reader.

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write(csv::Record::from_list(["1", "x,y"]));
    assert(w.as_str() == "1,\"x,y\"\n");
    
  • pub fn serialize<T: ToRecord>(self, x: T) throws (CsvError)§

    Source

    A row of a #[derive(Csv)] type; the first call writes the header first when the dialect has one.

    nabi
    #[derive(csv::Csv)]
    struct P { x: i32; tag: str | none; }
    
    let w = csv::Dialect::new().to_buffer();
    try! w.serialize(P { x: 1, tag: "a" });
    try! w.serialize(P { x: 2, tag: none });
    assert(w.as_str() == "x,tag\n1,a\n2,\n");
    
  • pub fn flush(self) throws (CsvError)§

    Source

    Pass what is buffered to the stream and flush it.

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.flush();
    
  • pub fn as_str(self) -> str§

    Source

    The text written so far (a memory writer's output).

    nabi
    let w = csv::Dialect::new().to_buffer();
    try! w.write_record(["a"]);
    assert(w.as_str() == "a\n");