To demonstrate how to read and write a binary file, we will create a little custom binary protocol. It will start with what is called a magic number, that is, a certain hardcoded value. Our magic number will be the binary representation of the MyProtocol string. We can put a b before the string to tell Rust that we want the text to be represented as a binary slice (&[u8]) instead of a string slice(&str) [26].
Many protocols and files start with magic numbers to indicate what they are. For example, the internal headers of .zip files start with the magic hex numbers 0x50 and 0x4B. These represent the initials PH in ASCII, which is short for the name of its creator Phil Katz. Another example would be PDF; it starts with 0x25, 0x50, 0x44, and 0x46, which stands for PDF%, followed by a version number.
Afterward, we follow it by the binary representation of either LE or BE to tell the reader the endianness of the rest of the data [31]. Finally, we have the...