Langsung ke konten
Semua insights

Academy

From Zero to Running: Creating, Building, and Testing Rust Projects with Cargo

Cargo standardizes project creation, building, running, and testing into four simple commands, letting Rust developers go from zero to running code in seconds.

3 menit baca

From Zero to Running: Creating, Building, and Testing Rust Projects with Cargo
Di halaman ini

The distance between "I want to write a Rust program" and "I have a running Rust program" should be measured in seconds, not hours. In C++, setting up a new project means creating directories, writing a Makefile or CMakeLists.txt, configuring include paths, and hoping your compiler flags are correct. In Rust, it's one command: cargo new.

This isn't just convenience—it's a philosophy. Cargo's standardized project structure means every Rust project looks the same, every build works the same way, and every new team member can navigate unfamiliar code immediately. The ceremony-to-productivity ratio is the best in any compiled language.

At RantAI, new engineers write and test their first Rust code within minutes of cloning a repository, because Cargo eliminates every setup friction point. This article, drawn from Chapter 7, Section 7.2 of our guide "The Rust Programming Language," walks through the complete create-build-run-test workflow.

Step 1: Create#

Bash
cargo new my_project
cd my_project

You now have:

Code
my_project/
├── Cargo.toml
└── src/
    └── main.rs

main.rs contains a Hello World. Cargo.toml contains the project metadata. You're already ready to build.

For a library instead of a binary:

Bash
cargo new my_lib --lib

This creates src/lib.rs instead of src/main.rs, with a sample test included.

Step 2: Build#

Bash
cargo build           # Debug build: fast compile, debug symbols
cargo build --release  # Release build: optimized, production-ready

Debug builds live in target/debug/. Release builds in target/release/. Cargo tracks what changed and only recompiles affected files (incremental compilation).

Step 3: Run#

Bash
cargo run
# Output: Hello, world!

One command: compile + execute. During development, you'll use this constantly. Add --release for optimized execution.

Step 4: Test#

Bash
cargo test

Rust has a built-in testing framework. Tests live alongside your code:

Rust
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    #[should_panic]
    fn it_panics() {
        panic!("This test should panic");
    }
}

cargo test finds and runs every function annotated with #[test]. Tests are compiled conditionally—they don't exist in release builds.

The Complete Workflow#

Bash
cargo new my_project && cd my_project  # Create
cargo build                             # Compile
cargo run                               # Execute
cargo test                              # Verify

Four commands, zero configuration files to write, zero dependencies to manage manually. From nothing to a tested, running program in under a minute.

Broader Implications: Onboarding in Minutes#

At RantAI, our onboarding process for new Rust engineers is: clone the repo, run cargo test, start coding. No environment setup guide, no "works on my machine" issues, no build system archaeology.

Practical Applications & Strategic Takeaways#

  • For beginners: Memorize cargo new, cargo run, cargo test. They're your daily workflow.

  • For team leads: Cargo's conventions mean every project has the same structure. Code review is faster when layout is predictable.

  • For educators: Start every Rust lesson with cargo new. Students write and test code in the first five minutes.

Our Commitment to Open Knowledge#

RantAI is committed to open education. The Cargo workflow is covered in Chapter 7, Section 7.2 of our guide, "The Rust Programming Language," freely available online.

Explore these concepts further: https://trpl.rantai.dev

Support Our Mission & Get Your Handbook#

How fast did you go from cargo new to a working project? Share your onboarding stories!

#RustLang #Cargo #ProjectSetup #Testing #RantAI #LearnRust #DeveloperWorkflow #IncrementalCompilation #Productivity #GettingStarted

Artikel berikutnya

Dependency Management with Cargo: Adding Crates Without the Headaches