rust – How to write a slice into a file?


I have code which interracts with C via bindings. In it I have this:

let a1: *const i8 = get_data();
let size1 = get_size_of_data();

let a2 = ::std::slice::from_raw_parts(a2, size1);

I need to write a2 or a1 into a binary file. The size or length of the data is known.

I tried:

  let mut f1 = File::create("my_file.data").unwrap();
  // ??? f1.write_all(a2).expect("unable to write binary data to file");
  // ??? f1.write_all(a2.as_bytes).expect("unable to write binary data to file");

and nothing has compiled.

How to do it?