1

I have the following struct:

pub struct Employee {
     id: i128,
     pub name: String,
     pub department: String,
}

Part of my application collects Employee structs into a vector for storage. I would like to write all the Employee structs in the vector to a file. My plan was to do the following:

let mut directory = File::create("employee.txt").expect("unable to make file");

for e in &employees {
    directory.write(e).expect("cannot write data");
}

Unfortunately write only takes &[u8] and I'm totally flummoxed on how to approach this problem. It doesn't seem possible to convert the struct values to &[u8] and I'm unsure if extracting each value within the struct to write them to the file is possible.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

0 Answers0