官术网_书友最值得收藏!

How it works...

Let's go through the code to understand the execution flow.

cargo is described in depth in Chapter 2Managing Projects with Cargo, in this book.

The initial snippet is generated when cargo new hello-world is executed in step 1. As a project of type binary, a main function is required and rustc will be looking for it. Upon calling cargo run, cargo orchestrates compilation (with rustc) and linking (msvc on Windows, cc on *nix) and runs the resultant binary via its entry point: the main function (step 5). 

In the function we create in step 3, we write a series of print!/println!/eprintln! statements, which are Rust macros. These macros facilitate the writing to the standard output or standard error channels of a command-line application and include additional arguments. In fact, if arguments are missing, the compiler won't compile the program.

Rust's macros work directly on the syntax tree of the language, providing type safety and the ability to check the parameters and arguments. Therefore, they can be seen as a function call with a few special abilities—but more on that in Chapter 6, Expressing Yourself with Macros.   

The various arguments and the template string are combined using formatters, a powerful way to add real variables to the output without the need of concatenations or similar workarounds. This will reduce the number of allocations, considerably improving performance and memory efficiency. There is a wide range of how to format data types; to understand it more deeply, check out Rust's excellent documentation (https://doc.rust-lang.org/std/fmt/). 

The last step then shows the output that the various combinations produced.

We've successfully learned to work with the command line I/O. Now, let's move on to the next recipe.

主站蜘蛛池模板: 普兰店市| 松潘县| 卫辉市| 桐乡市| 康定县| 华蓥市| 鲁甸县| 临汾市| 信阳市| 峨眉山市| 张掖市| 当雄县| 阿巴嘎旗| 建德市| 许昌市| 云安县| 阿克苏市| 二连浩特市| 辽源市| 晋州市| 肃南| 慈溪市| 乌鲁木齐县| 光山县| 南华县| 莲花县| 道孚县| 岳阳市| 凤台县| 奉节县| 门源| 虹口区| 桓台县| 津市市| 沈丘县| 绍兴市| 从化市| 合肥市| 望谟县| 佳木斯市| 涞水县|