Rust Cargo

Rust's build system and package manager

๐Ÿ“ฆ What is Cargo?

Cargo is Rust's built-in package manager and build system. It handles project creation, dependency management, compilation, testing, and publishing packages to make Rust development seamless and efficient.


# Create a new Rust project
cargo new my_project
cd my_project
cargo run
                                    

Essential Cargo Commands

๐Ÿ†•

cargo new

Create a new Rust project

cargo new hello_world
cargo new --lib my_library
๐Ÿ”จ

cargo build

Compile the project

cargo build
cargo build --release
โ–ถ๏ธ

cargo run

Build and run the project

cargo run
cargo run --bin my_binary
๐Ÿงช

cargo test

Run project tests

cargo test
cargo test test_name

๐Ÿ”น Cargo.toml Configuration

The Cargo.toml file contains project metadata and dependencies:

[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
authors = ["Your Name <[email protected]>"]
description = "A sample Rust project"
license = "MIT"

[dependencies]
serde = "1.0"
tokio = { version = "1.0", features = ["full"] }
rand = "0.8"

[dev-dependencies]
assert_cmd = "2.0"

[[bin]]
name = "my_binary"
path = "src/bin/main.rs"

Key sections:

  • [package]: Project metadata
  • [dependencies]: Runtime dependencies
  • [dev-dependencies]: Development-only dependencies
  • [[bin]]: Binary target configuration

๐Ÿ”น Managing Dependencies

Add and manage external crates easily:

# Add a dependency
cargo add serde
cargo add tokio --features full

# Remove a dependency
cargo remove serde

# Update dependencies
cargo update
cargo update serde

๐Ÿ”ธ Using Dependencies in Code

// Cargo.toml: serde = { version = "1.0", features = ["derive"] }
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u32,
}

fn main() {
    let person = Person {
        name: "Alice".to_string(),
        age: 30,
    };
    
    let json = serde_json::to_string(&person).unwrap();
    println!("JSON: {}", json);
}

๐Ÿ”น Project Structure

Cargo follows a standard project layout:

my_project/
โ”œโ”€โ”€ Cargo.toml          # Project configuration
โ”œโ”€โ”€ Cargo.lock          # Dependency lock file
โ”œโ”€โ”€ src/                # Source code
โ”‚   โ”œโ”€โ”€ main.rs         # Binary entry point
โ”‚   โ”œโ”€โ”€ lib.rs          # Library entry point
โ”‚   โ””โ”€โ”€ bin/            # Additional binaries
โ”‚       โ””โ”€โ”€ helper.rs
โ”œโ”€โ”€ tests/              # Integration tests
โ”‚   โ””โ”€โ”€ integration_test.rs
โ”œโ”€โ”€ benches/            # Benchmarks
โ”‚   โ””โ”€โ”€ my_benchmark.rs
โ””โ”€โ”€ examples/           # Example code
    โ””โ”€โ”€ example.rs

Directory purposes:

  • src/: Main source code
  • tests/: Integration tests
  • benches/: Performance benchmarks
  • examples/: Usage examples

๐Ÿ”น Build Profiles

Customize compilation settings for different scenarios:

[profile.dev]
opt-level = 0      # No optimization
debug = true       # Include debug info
panic = "unwind"   # Unwind on panic

[profile.release]
opt-level = 3      # Maximum optimization
debug = false      # No debug info
lto = true         # Link-time optimization
codegen-units = 1  # Single codegen unit
# Build with different profiles
cargo build                    # Uses dev profile
cargo build --release         # Uses release profile
cargo run --release          # Run optimized build

๐Ÿง  Test Your Knowledge

Which command creates a new Rust library project?