I can write binary code to file with Rust. However, when I create a file, the file created is a text file, not binary. I can create a binary file with C++ like this:
ofstream is("1.in", ofstream::binary | ofstream::out | ofstream:: trunc);
How about in Rust? This is my attempt:
struct IndexDataStructureInt {
row: u32,
key_value: u32,
}
let mut index_arr: Vec<IndexDataStructureInt> = Vec::new();
// doing something push 100 IndexDataStructureInt to index_arr
let mut fileWrite = File::create(tableIndexName).unwrap();
for i in 0..index_arr.len() {
write!(
fileWrite,
"{:b}{:b}",
index_arr[i].row, index_arr[i].key_value
);
}
After running this code, it writes 200 u32 integer binary number to the file tableIndexName. However, the file size is not 800bytes. It is about 4KB.
snake_casefor variables, methods, macros, and fields;UpperCamelCasefor types and enum variants; andSCREAMING_SNAKE_CASEfor statics and constants. Usefile_writeandtable_index_nameinstead, please.