Rust in Action Notes Chapter 7 Files and Storage

  1. serdeThe library provides serialization (serialize) and deserialization (deserialize) features. Generated by derive, the custom type of rust (struct) can be converted into a variety of commonly used compatible types such as JSON, CBOR, and bincode for transmission between networks. Among them, JSON is a more commonly used type, which is characterized by both human-readable and machine-readable. The other two are only machine-readable. Page 215 gives an example;
  2. Retrieve the content of a file using the hexdump method to convert a series of byte streams into two hexadecimal numbers (that is, exactly one byte). This view is called fview (file view file view). Table 7.2 shows the input and output of a fviewTable 7.2
  3. When a multi-line string is used as a raw string (raw string), there is no need to escape the double quotes. The pattern of the raw string is a prefix r, a separator #, and an additional prefix bmeans that the following string should be regarded as bytes (&[u8]) instead of UTF-8 characters (&str). The following figure is an exampleraw string
  4. f.read_exact(&mut buffer)The method transfers data from the source fto bufferthe middle, and stops when the buffer is full. If the buffer is longer than the data in f, an error will be returned; it std::env::args()is used to read the parameters of the command line;
  5. std::fs::OpenOptionsIt can be used to create writable files. Unlike File::openand File::create, it can customize some functions. The difference between open and create is shown in the following tabletable 7.3
  6. It is recommended to use Paththe library when processing file paths. There are three advantages: a. Clarify the intention of the developer, for example, the method can set the extension; b. Multi-platform compatibility, different operating systems cannot handle file paths, set_extension()for example, some operating systems are sensitive to the case of file names while others are not, and different operating systems use different path separators ;Pathstd::Stringstd::path::Pathtable 7.4
  7. Section 7.5 simply builds an in-memory database project, introduces the usage rules and calling paths of neutralization, and is a small but comprehensive software cargo.tomlengineering practice project, which is very inspiring for personal development of rust tools, Page222;libbin
  8. Section 7.6.1 introduces the usage of conditional compilation (conditional compilation). Adding above the statement that requires conditional compilation #[cfg(target_os="windows")]can make the statement compile only under the windows system. For non operation, there is no !=symbol, but #[cfg(not(...))]the compilation conditions that can be set include target_arch, target_env, etc., see Page227 for details;
  9. std::io::ErrorKind::UnexpectedEofThe error is an error reported when the program reads the end of the file. EOF is a 0 byte (0u8). There is no special mark at the end of the file to indicate that the file has ended, so this error will be thrown when the end of the file cannot be read;
  10. Section 7.7.3 introduces the order of writing binary data to the disk. You can specify the big-endian/little-endian writing method. Note that such i8, u8data does not need to specify big-endian and small-endian, because there is only one byte. You u32, f64can specify it like this, and the writing effect can be seen on Page 233;
  11. Section 7.7.3 introduces the verification method for writing to the disk. The three common methods are parity bit, cyclic redundancy method (CRC32), and cryptographic hash function. The comparison between them is shown in the following tabletable 7.8

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131396607