files_old/
files_old.rs

1use std::fs::File;
2use std::io::prelude::*;
3
4fn main() {
5    // Our Data:
6    let data = vec![
7        (1, "Hello".as_bytes().to_vec()),
8        (2, ", ".as_bytes().to_vec()),
9        (4, "world".as_bytes().to_vec()),
10        (1, "!".as_bytes().to_vec()),
11    ];
12
13    // Note that one should not panic!() in real life.
14    let encoded_data = match kvds::encode(data) {
15        Ok(d) => d,
16        Err(e) => panic!("Err: {:?}", e),
17    };
18
19    // Create the file...
20    // One could also open a file using File::open().
21    let mut file = match File::create("data") {
22        Ok(f) => f,
23        Err(e) => panic!("Err: {:?}", e), // Again, DO NOT PANIC!() IRL.
24    };
25
26    // We write the data, of type Vec<u8>, to the file.
27    match file.write_all(&encoded_data) {
28        Err(e) => panic!("Err: {:?}", e), // Do something other than panic!()
29        _ => (),
30    }
31
32    // Open the file.
33    let mut file = match File::open("data") {
34        Ok(f) => f,
35        Err(e) => panic!("Err: {:?}", e), // You know.
36    };
37
38    // We read the data back out, to the variable `buffer`.
39    let mut buffer = Vec::<u8>::new();
40    match file.read_to_end(&mut buffer) {
41        Err(e) => panic!("Err: {:?}", e), // ^^
42        _ => (),
43    }
44
45    // Decode the data.
46    let decoded_data = match kvds::decode(buffer) {
47        Ok(d) => d,
48        Err(e) => panic!("Err: {:?}", e),
49    };
50
51    // We print it.  It should look like a Vec<(u8, Vec<u8>)>, same as the original `data`!
52    println!("{:?}", decoded_data);
53}