Function encode_to_file

Source
pub fn encode_to_file(
    name: &str,
    data: Vec<(u8, Vec<u8>)>,
) -> Result<(), EncodeToFileError>
Expand description

Encodes a key-value list to a file.

§Example

let data = vec![
    (1, "Hello".as_bytes().to_vec()),
    (2, ", ".as_bytes().to_vec()),
    (4, "world".as_bytes().to_vec()),
    (1, "!".as_bytes().to_vec()),
];

kvds::encode_to_file("data", data)?;

§Errors

This function returns an error if something goes wrong with creating or writing to a file, or an error occurs in encoding the data.

Examples found in repository?
examples/files.rs (line 12)
1fn main() {
2    // Our Data:
3    let data = vec![
4        (1, "Hello".as_bytes().to_vec()),
5        (2, ", ".as_bytes().to_vec()),
6        (4, "world".as_bytes().to_vec()),
7        (1, "!".as_bytes().to_vec()),
8    ];
9
10    // Write to file.
11    // Note that one should not panic!() in real life.
12    match kvds::encode_to_file("data", data) {
13        Err(e) => panic!("Err: {:?}", e),
14        _ => (),
15    }
16
17    // Read from file.
18    let decoded_data = match kvds::decode_from_file("data") {
19        Ok(d) => d,
20        Err(e) => panic!("Err: {:?}", e), // Again, DO NOT PANIC!() IRL.
21    };
22
23    // Print it.  It should look like a Vec<(u8, Vec<u8>)>, same as the original `data`!
24    println!("{:?}", decoded_data);
25}