use std::fs::File; use std::io::prelude::*; fn main() { // Our Data: 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()), ]; // Note that one should not panic!() in real life. let encoded_data = match kvds::encode(data) { Ok(d) => d, Err(e) => panic!("Err: {:?}", e), }; // Create the file... // One could also open a file using File::open(). let mut file = match File::create("data") { Ok(f) => f, Err(e) => panic!("Err: {:?}", e), // Again, DO NOT PANIC!() IRL. }; // We write the data, of type Vec, to the file. match file.write_all(&encoded_data) { Err(e) => panic!("Err: {:?}", e), // Do something other than panic!() _ => (), } // Open the file. let mut file = match File::open("data") { Ok(f) => f, Err(e) => panic!("Err: {:?}", e), // You know. }; // We read the data back out, to the variable `buffer`. let mut buffer = Vec::::new(); match file.read_to_end(&mut buffer) { Err(e) => panic!("Err: {:?}", e), // ^^ _ => (), } // Decode the data. let decoded_data = match kvds::decode(buffer) { Ok(d) => d, Err(e) => panic!("Err: {:?}", e), }; // We print it. It should look like a Vec<(u8, Vec)>, same as the original `data`! println!("{:?}", decoded_data); }