- It can be
rustc
used to.rs
compile 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 generatedcargo run
binary.rs
file./target
; - Section 2.2 talks about how to declare variables, which is relatively basic;
{}
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 ;- 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/f64
std::cmp::PartialEq
std::cmp::Eq
a-b < e
- Section 2.4 talks about some commonly used keywords for control flow, such as
for, while, loop
etc.; the use ofif else
and , the pattern matching function, and the common usage of match can also be found in rust_by_example, match ;match
match
- 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, - 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; grep-lite
Section 2.9 reviewed the looping method through a simplified grep projectfor
, and incidentally talked about the differences and uses of different string types in the rust language, including thechar, [u8], Vec<u8>, std::ffi::OSString, std::path::Path
keyString和str,&str
differences;- 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;
- Section 2.11 talks about how to introduce a third-party code library, you can pass it
cargo add {some_crate}
, or you cancargo.toml
add 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 ; - Section 2.11 also introduces the use of the project root directory to
cargo doc
create 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 therustup doc
offline version of the rust documentation in the local browser, and the usage guide for rustup can be found at rustup-guide ; - Section 2.12 introduces a useful third-party library
clap
for 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; - Section 2.13 introduces the method of file IO, the standard library
std::fs::File
andstd::io::BufReader
the commonly used file IO library, and the method of obtaining input from standard inputstd::io::stdin
;
Rust in Action Notes Chapter 2 Rust Language Basics
Guess you like
Origin blog.csdn.net/Mint2yx4/article/details/130991016
Recommended
Ranking