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

Subsections of Cargo

Cargo Offline

Cargo OFFLINE

Для создания локального сСрвСра ΠΌΠΎΠΆΠ½ΠΎ ΡΠΊΠ°Ρ‡Π°Ρ‚ΡŒ всС ΠΏΠ°ΠΊΠ΅Ρ‚Ρ‹ Cargo с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚Π° Panamax.

cargo install --locked panamax
panamax init ~/test/my-mirror

НуТно Π·Π°ΠΉΡ‚ΠΈ Π² ΠΏΠ°ΠΏΠΊΡƒ my-mirror, ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ настройки Π² Ρ„Π°ΠΉΠ»Π΅ mirror.toml. И Π΄Π°Π»Π΅Π΅ Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ ΡΠΈΠ½Ρ…Ρ€ΠΎΠ½ΠΈΠ·Π°Ρ†ΠΈΡŽ:

panamax sync ~/test/my-mirror

Π”Π°Π»Π΅Π΅, ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡƒΠ±Π»ΠΈΠΊΠΎΠ²Π°Ρ‚ΡŒ Π·Π΅Ρ€ΠΊΠ°Π»ΠΎ ΠΏΠΎ Π²Π΅Π± Ρ‡Π΅Ρ€Π΅Π· встроСнный сСрвСр (ΠΏΠΎ ΠΏΠΎΡ€Ρ‚Ρƒ 8080):

panamax serve ~/test/my-mirror

На индСкс страницС сСрвСра Π±ΡƒΠ΄Π΅Ρ‚ справка ΠΏΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡŽ Rust ΠΊΠ»ΠΈΠ΅Π½Ρ‚ΠΎΠ² ΠΊ Π·Π΅Ρ€ΠΊΠ°Π»Ρƒ. Π’ частности, посрСдством создания Ρ„Π°ΠΉΠ»Π° настроСк ~/.cargo/config :

[source.my-mirror]
registry = "http://panamax.local/crates.io-index"
[source.crates-io]
replace-with = "my-mirror"

Cargo Workspaces

Cargo Workspaces

ОбъСдинСниС ΠΊΠΎΠ΄Π° Π² 1 ΠΏΡ€ΠΎΠ΅ΠΊΡ‚. Π’ Cargo.toml Π½ΡƒΠΆΠ½ΠΎ ΠΎΠ±ΡŠΡΠ²ΠΈΡ‚ΡŒ Π΅Π³ΠΎ структуру:

[package]
name = "ardan_workspace"
version = "0.1.0"
edition = "2021"

[dependencies]

[workspace]
members = [ 
  "session1/hello_world",
  "session1/variables",
]

ПослС этого ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²Π»ΡΡ‚ΡŒ ΠΏΠΎΠ΄ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ Π² members, Ρ‡Π΅Ρ€Π΅Π· cargo new <ΠΏΠΎΠ΄Ρ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°>.

Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ Π² Workspace

Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ вмСсто Π±ΠΈΠ½Π°Ρ€Π½ΠΈΠΊΠ° - `cargo new –lib examplelib

ΠŸΡ€ΠΎΠΏΠΈΡΠ°Ρ‚ΡŒ Π² Ρ„Π°ΠΉΠ»Π΅ cargo.toml Ρƒ Π±ΠΈΠ½Π°Ρ€Π½ΠΎΠΉ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ Π² workspace Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡ‚ΡŒ:

<..>

[dependencies]
examplelib = { path = "../examplelib"}

Π”Π°Π»Π΅Π΅, Π² ΠΊΠΎΠ΄ Π±ΠΈΠ½Π°Ρ€Π½ΠΎΠΉ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ Π²ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΈΠ· Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ:

use examplelib::function01; // фукнкция Π΄ΠΎΠ»ΠΆΠ½Π° Π±Ρ‹Ρ‚ΡŒ ΠΏΡƒΠ±Π»ΠΈΡ‡Π½ΠΎΠΉ (pub fn)