Skip to content

0x03 - Cargo

MasakiMu319

In Rust communities, “crate” is often discussed like a package unit. You can think of a package as a project/software package wrapper.

Rust projects commonly involve two crate types:

cargo new creates a new project. By default it creates an executable binary package (--bin is default and can be omitted).

cargo run compiles in debug mode and then runs. With --release, Rust applies heavy optimization for performance.

mod keyword: declares a module and tells the compiler to include module files.

use keyword: imports types/functions from a module into current scope.

Difference between mod and creating lib.rs

Why does lib.rs reduce many mod declarations in main.rs?

Once modules are declared in lib.rs, main.rs can directly use them without repeating top-level mod declarations. This keeps main.rs focused on entry logic.

Basics

After pulling a remote Rust project, cargo build will automatically resolve dependencies and build.

Cargo manages dependencies and versions via Cargo.toml (similar role to pyproject.toml in Python). During build, it also generates Cargo.lock (similar idea to uv.lock), containing more specific resolved dependency metadata.

To update dependency versions, run cargo update; otherwise Cargo uses the versions declared/resolved from current configuration.

Cargo Project Layout Conventions

.
├── Cargo.lock
├── Cargo.toml
├── src/
│   ├── lib.rs
│   ├── main.rs
│   └── bin/
│       ├── named-executable.rs
│       ├── another-executable.rs
│       └── multi-file-executable/
│           ├── main.rs
│           └── some_module.rs
├── benches/
│   ├── large-input.rs
│   └── multi-file-bench/
│       ├── main.rs
│       └── bench_module.rs
├── examples/
│   ├── simple.rs
│   └── multi-file-example/
│       ├── main.rs
│       └── ex_module.rs
└── tests/
    ├── some-integration-tests.rs
    └── multi-file-test/
        ├── main.rs
        └── test_module.rs

If a binary/example/bench/integration-test has multiple source files, place them in a subdirectory with a main.rs. The executable name follows the folder name.

Previous
TEI Customization and Exploration of Rust Support for ML
Next
Local Model Inference Speed Benchmark