Cargo
Use Cargo for managing projects.
cargo new test_project // create project with binary file src/main.rs
// OR
cargo new test_project --lib // create project with library file src/lib.rs
cd test_project
Source code is in src folder.
cargo build # build project for debug, do not run
cargo run # build & run project
cargo check # fast compiling
cargo build --release # slow build with optimizations for speed of release version
Documentation of methods & traits used in code can be compiled an opened in browser with Cargo command:
cargo doc --open
Cargo Examples
Create “examples” folder beside the “src” folder. Create a file named, for example, “variables.rs” and place some test code in the folder. Then in the project root folder run:
cargo run --example variables
Cargo Watch
This will compile+build the code in examples folder, file “variables.rs”. Very convenient to try test different stuff. For live development do:
cargo watch -q -c -x 'run -q --example variables'
-x - rerun upon code change -c - clear terminal each time -q - quiet mode
Panic Response
Π ΠΎΡΠ²Π΅Ρ Π½Π° ΠΏΠ°Π½ΠΈΠΊΡ, ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° ΡΠ°Π·ΠΌΠ°ΡΡΠ²Π°Π΅Ρ ΡΡΠ΅ΠΊ (unwinding) - ΠΏΡΠΎΡ
ΠΎΠ΄ΠΈΡ ΠΏΠΎ ΡΡΠ΅ΠΊΡ ΠΈ Π²ΡΡΠΈΡΠ°Π΅Ρ Π΄Π°Π½Π½ΡΠ΅ Π²ΡΠ΅Ρ
ΡΡΠ½ΠΊΡΠΈΠΉ. ΠΠ»Ρ ΡΠΌΠ΅Π½ΡΡΠ΅Π½ΠΈΡ ΡΠ°Π·ΠΌΠ΅ΡΠ° ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΎΡΡΠΎ ΠΎΡΠΊΠ»ΡΡΠ°ΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ Π±Π΅Π· ΠΎΡΠΈΡΡΠΊΠΈ - abort. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ Π² ΡΠ°ΠΉΠ»Π΅ Cargo.toml Π½Π°Π΄ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π² ΡΠ°Π·Π΄Π΅Π»Ρ [profile]
:
[profile.release]
panic = 'abort'
Cargo Clippy linter
Example of Clippy config:
cargo clippy --fix -- \
-W clippy::pedantic \
-W clippy::nursery \
-W clippy::unwrap_used \
-W clippy::expect_used
Clippy has a markdown book:
cargo install mdbook
# Run from top level of your rust-clippy directory:
mdbook serve book --open
# Goto http://localhost:3000 to see the book
Important Cargo libs
- Tokio - async runtime
- Eyre & color-eyre - type signatures, error report handling
- Tracing - collect diagnostic info
- Reqwest - HTTP requests library, async
- Rayon - data parallelism library, without data races
- Clap - commandline passing library
- SQLX - compile-checked SQL, async
- Chrono - time/date library
- EGUI - web-gui @60FPS, runs in Webassembly
- Yew.rs - web-library like React.js