Thanks to visit codestin.com
Credit goes to docs.rs

Module io

Module io 

Source
Expand description

Helpers and type definitions for extended I/O functionality

The io module contains a number of types and functions to assist with common I/O activities, such a slurping a file by lines, or writing a collection of Serializable objects to a path.

The two core parts of this module are teh Io and DelimFile structs. These structs provide methods for reading and writing to files that transparently handle compression based on the file extension of the path given to the methods.

§Example

use std::{
    default::Default,
    error::Error
};
use fgoxide::io::{Io, DelimFile};
use serde::{Deserialize, Serialize};
use tempfile::TempDir;

#[derive(Debug, Deserialize)]
struct SampleInfo {
    sample_name: String,
    count: usize,
    gene: String
}

fn main() -> Result<(), Box<dyn Error>> {
    let tempdir = TempDir::new()?;
    let path = tempdir.path().join("test_file.csv.gz");

    let io = Io::default();
    let lines = ["sample_name,count,gene", "sample1,100,SEPT14", "sample2,5,MIC"];
    io.write_lines(&path, lines.iter())?;

    let delim = DelimFile::default();
    let samples: Vec<SampleInfo> = delim.read(&path, b',', false)?;
    assert_eq!(samples.len(), 2);
    assert_eq!(&samples[1].sample_name, "sample2");
    Ok(())
}

Structs§

DelimFile
Unit-struct that contains associated functions for reading and writing Structs to/from delimited files. Structs should use serde’s Serialize/Deserialize derive macros in order to be used with these functions.
DelimFileReader
A struct that wraps a csv Reader and provides methods for reading one record at a time. It also implements Iterator.
DelimFileWriter
A struct that wraps a csv Writer and provides methods for writing single records as well as multiple records from an iterator.
Io
Unit-struct that contains associated functions for reading and writing Structs to/from unstructured files.