serde
The 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;- 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 fview
- 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 prefixb
means that the following string should be regarded as bytes (&[u8]) instead of UTF-8 characters (&str). The following figure is an example f.read_exact(&mut buffer)
The method transfers data from the sourcef
tobuffer
the middle, and stops when the buffer is full. If the buffer is longer than the data in f, an error will be returned; itstd::env::args()
is used to read the parameters of the command line;std::fs::OpenOptions
It can be used to create writable files. UnlikeFile::open
andFile::create
, it can customize some functions. The difference between open and create is shown in the following table- It is recommended to use
Path
the 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 ;Path
std::String
std::path::Path
- 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.toml
engineering practice project, which is very inspiring for personal development of rust tools, Page222;lib
bin
- 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; std::io::ErrorKind::UnexpectedEof
The 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;- 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, u8
data does not need to specify big-endian and small-endian, because there is only one byte. Youu32, f64
can specify it like this, and the writing effect can be seen on Page 233; - 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 table
Rust in Action Notes Chapter 7 Files and Storage
Guess you like
Origin blog.csdn.net/Mint2yx4/article/details/131396607
Ranking