Book Mastering Rust ⬆
Directory mastering-rust\ gathers Rust code examples from Sharma's book Mastering Rust.It also includes several build scripts (batch files, Make scripts and more) for experimenting with Rust on a Windows machine. |
Code examples in directory mastering-rust\
can be build/run with the following build tools in addition to cargo.exe
, the standard tool 1 for Rust projects :
Build tool | Build file | Parent file | Environment(s) |
---|---|---|---|
cmd.exe |
build.bat build.properties a) |
Windows only | |
cargo.exe |
Cargo.toml |
Any b) | |
make.exe |
Makefile |
Makefile.inc |
Any |
b) Here "Any" means "tested on Windows, Cygwin, MSYS2 and Unix".
The Chapter01
code example gives an overview of the Rust language through many small source files such as arrays.rs
, closures.rs
, enums.rs
and so on :
build.bat
2
The default main program arrays.rs
is defined in configuration file build.properties
:
> cd R:\mastering-rust\Chapter01 > build -verbose clean run Delete directory "target" Compile 1 Rust source file to directory "target" Execute program "target\arrays.exe" Number: 7 Float: 0.3
We can select another main program with option -main:<name>
, e.g.
> build -verbose -main:closures clean run Delete directory "target" Compile 1 Rust source file to directory "target" Execute program "target\closures.exe" 5 doubled is 10 Result from closure: 30
We can display the executed command lines with option -debug
, e.g.
> build -debug clean run [build] Options : _EDITION=2021 _TARGET=msvc _TIMER=0 _VERBOSE=0 [build] Subcommands: _CLEAN=1 _COMPILE=1 _DOC=0 _DUMP=0 _RUN=1 _TEST=0 [build] Variables : "CARGO_HOME=%USERPROFILE%\.cargo" [build] Variables : "GIT_HOME=C:\opt\Git" [build] Variables : "MSYS_HOME=C:\opt\msys64" [build] Variables : "RUSTUP_HOME=%USERPROFILE%\.rustup" [build] Variables : _CRATE_NAME=arrays _CRATE_TYPE=bin [build] Variables : _TARGET_TRIPLE="x86_64-pc-windows-msvc" [build] rmdir /s /q "R:\mastering-rust\Chapter01\target" [build] 00000000000000 Target : 'R:\mastering-rust\Chapter01\target\arrays.exe' [build] 20211025151633 Sources: 'R:\mastering-rust\Chapter01\src\*.rs' [build] _ACTION_REQUIRED=1 [build] "%USERPROFILE%\.cargo\bin\rustc.exe" -g --crate-name "arrays" --crate-type bin --edition 2021 --out-dir "R:\mastering-rust\Chapter01\target" --target "x86_64-pc-windows-msvc" "R:\mastering-rust\Chapter01\src\arrays.rs" [build] "R:\mastering-rust\Chapter01\target\arrays.exe" Number: 7 Float: 0.3 [build] _EXITCODE=0
The default main program is arrays.rs
is defined in configuration file Cargo.toml
:
> cargo clean & cargo run --bin=arrays Compiling Chapter01 v1.0.0 (R:\mastering-rust\Chapter01) Finished dev [unoptimized + debuginfo] target(s) in 2.85s Running `target\debug\arrays.exe` Number: 7 Float: 0.3
We can select another main program with option --bin:<name>
, e.g.
> cargo clean & cargo run --bin=closures Compiling Chapter01 v1.0.0 (R:\mastering-rust\Chapter01) Finished dev [unoptimized + debuginfo] target(s) in 2.20s Running `target\debug\closures.exe` 5 doubled is 10 Result from closure: 30
🔎 Alternatively one may also add the field
default-run
in the[package]
section ofCargo.toml
(see Target Selection in the online Cargo Book).> cargo --quiet run Number: 7 Float: 0.3
> make clean run "C:/opt/Git/usr/bin/rm.exe" -rf "target" "C:/opt/Git/usr/bin/rm.exe" -f "Cargo.lock" [ -d "target" ] || "C:/opt/Git/usr/bin/mkdir.exe" -p "target" "%USERPROFILE%/.cargo/bin/rustc.exe" --crate-name "arrays" --crate-type bin --edition 2018 --out-dir "target" --target "x86_64-pc-windows-msvc" src/arrays.rs target\arrays.exe Number: 7 Float: 0.3
2018
is the default Rust edition (see above session example); we can select another edition with variable EDITION
, e.g.
> make EDITION=2021 clean run "C:/opt/Git/usr/bin/rm.exe" -rf "target" "C:/opt/Git/usr/bin/rm.exe" -f "Cargo.lock" [ -d "target" ] || "C:/opt/Git/usr/bin/mkdir.exe" -p "target" "%USERPROFILE%/.cargo/bin/rustc.exe" --crate-name "arrays" --crate-type bin --edition 2021 --out-dir "target" --target "x86_64-pc-windows-msvc" src/arrays.rs target\arrays.exe Number: 7 Float: 0.3
Chapter02 ▴
WIP
WIP
WIP
Code example buggie
expects a natural number as argument (source file src\main.rs
) :
> cargo -q run Missing argument. > cargo -q run 10 Fibonacci number at 10 is 34
Command build.bat run
generates the same output :
> build -verbose run Compile 1 Rust source file to directory "target" Execute program "target\main.exe" Fibonacci number at 10 is 34
Code example rr_demo
depends on Crate rand
as defined in configuration file Cargo.toml
(source file src\main.rs
).
> cargo -q run 0 3 7 9 5 2 4 1 6 8 Hello, world!
Footnotes ▴
[1] cargo
commands ↩
- cargo.exe is actually both a package manager and a build tool for the Rust language; subcommands are organized is several groups :
[2] build
subcommands ↩