Rust in Action Notes Chapter 2 Rust Language Basics

  1. It can be rustcused to .rscompile a file with a single suffix, and its usage is similar to gcc, which requires that the file must have a main function, and it will be a binary file after compilation; in most cases, rust projects are managed by cargo, and the function is to call rustc to compile the file while adding a lot of parameters, put the generated binary file in a folder, and finally run the generated cargo runbinary .rsfile ./target;
  2. Section 2.2 talks about how to declare variables, which is relatively basic;
  3. {}Section 2.3 talks about some grammars of numbers, and the most used ones may be some usages in structured output numbers , such as {:>5b}right alignment, width 5, binary output, see rust_by_example, print for detailed use cases ;
  4. There are many types of numbers directly, and different types cannot be directly compared, and the compiler can be required to make corresponding conversions through the or method; the comparison of floating-point numbers may cause errors due to the limitations of computer binary expressions, so the etc. type only implements features, while other digital types also implement features. In practical applications, comparisons of floating-point numbers should be avoided as much as possible, or this mode should be used to .into()ensure that the error is within the allowable range;.try_into()f32/f64std::cmp::PartialEqstd::cmp::Eqa-b < e
  5. Section 2.4 talks about some commonly used keywords for control flow, such as for, while, loopetc.; the use of if else and , the pattern matching function, and the common usage of match can also be found in rust_by_example, match ;matchmatch
  6. In section 2.7, by implementing one Mandelbrot set, that is, drawing an image to review the previous structured output, match matching and other functions,
  7. Section 2.8 talks about slightly more complex function declarations, including functions with lifecycles. Pay attention to the lifecycle suppression effect, which <'a:'b, 'b>means that the lifecycle of 'a is not shorter than the lifecycle of 'b. For specific examples, see rust_by_example, lifetime_coercion ; also talk about function declarations with template parameters T;
  8. grep-liteSection 2.9 reviewed the looping method through a simplified grep project for, and incidentally talked about the differences and uses of different string types in the rust language, including the char, [u8], Vec<u8>, std::ffi::OSString, std::path::Pathkey String和str,&strdifferences;
  9. Section 2.10 talks about the difference between array and vector arrays, and also introduces the concept of slices. Generally, array slices ([T]) or slice references (&[T]) are called slices. There are obvious differences between the signatures of arrays ([T; n]) and slices ([T]). It further explains that arrays are data structures on the stack, and their size is determined at compile time;
  10. Section 2.11 talks about how to introduce a third-party code library, you can pass it cargo add {some_crate}, or you can cargo.tomladd the corresponding library in it. Adding a library here can be more flexible, such as specifying the version of the library or some features, etc., see cargo reference for details ;
  11. Section 2.11 also introduces the use of the project root directory to cargo doccreate project documentation, and the generated documentation is located in ./target/doc/{project_name}/index; at the same time, it briefly introduces the rustup tool, cargo manages projects, rustup manages the rust toolchain and allows users to use different Rust compiler versions, input can open the rustup docoffline version of the rust documentation in the local browser, and the usage guide for rustup can be found at rustup-guide ;
  12. Section 2.12 introduces a useful third-party library clapfor processing command line input parameters. From then on, some parameters can be customized, as well as the library’s usage instructions. The clap documentation is in docs.rs/clap ; the clap version in the book is 2, and it has reached version ^4, so some APIs should be deprecated, and you need to read the documentation to learn;
  13. Section 2.13 introduces the method of file IO, the standard library std::fs::Fileand std::io::BufReaderthe commonly used file IO library, and the method of obtaining input from standard input std::io::stdin;

Guess you like

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