:#/ GSLF Journal
← :#/ GSLF Journal

Rustlers Atom 0.3 - Cargo

On this page

<figure class="video" style="margin:0;">
<div style="position:relative;width:100%;padding-bottom:56.25%;height:0;overflow:hidden;">
<iframe src="https://www.youtube.com/embed/oHZdfblUXeA?si=R6h6V_hKtJ-77kF7"
title="YouTube video player"
style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen></iframe>
</div>
</figure>

Cargo is Rust’s project+build+package manager in one. You describe your app or library in Cargo.toml, put code under src/, and let Cargo handle compilation, tests, docs, and dependencies reproducibly.

Build & Run#

Make a new binary crate (has a main):

cargo new hello cd hello`

Make a library instead:

cargo new mylib --lib

If you’re turning an existing folder into a project, use:

shell
cargo init

Build once to prime the target dir with:

shell
cargo build

This produces a debug build (fast to compile, slower to run). For optimized binaries:

cargo build --release

Artifacts land in target/debug/ or target/release/. Use cargo clean to wipe builds if you need a fresh compile.

You can run your binary directly with cargo for semplicity. Pass args after --:

cargo run
cargo run -- --port 8080

When you’re iterating, you can choose to compile without producing binaries. The command below catches errors quickly and keeps your edit-compile loop snappy.

cargo check

Test (unit, integration, selection)#

Run all tests:

cargo test

Run only tests matching a filter:

cargo test parser_handles_utf8

Integration tests live in tests/; unit tests usually sit next to code under src/.

Dependencies #

All depentency are managed by the Cargo.toml but you can manage it without editing by hand with this commands:

cargo add anyhow 
cargo add serde --features derive
cargo update
cargo tree

Lints, format and docs #

Keep formatting and lints consistent with:

cargo fmt cargo clippy

Build and open your documentation (including your crate’s docs):

cargo doc --open