SoFunction
Updated on 2025-04-16

Rust cargo command line tool usage sample tutorial

Introduction

cargoyesRustThe build system and package manager are responsible for creating projects, compiling code, managing dependencies, running tests, etc., and are the most commonly used tools in daily development.

Create a project

cargo new project_name      # Create a binary project (executable)cargo new --lib mylib       # create library project(供其它project调用)

It creates a project structure:

project_name/
├──         # Project information and dependency configuration└── src/
    └──        # Main entrance to the project( For the library)

Project structure and configuration files

is the core configuration file of the project, similar toJavaoforof

[package]
name = "my_project"
version = "0.1.0"
edition = "2025"
[dependencies]
rand = "0.8"     # Add dependencies

Commonly used commands

Compile the project

cargo build          # Build the project (debug mode)cargo build --release  # Build release model(optimization)

Run the project

cargo run

Run with parameters

cargo run -- arg1 arg2

Check for syntax and errors (not compile to generate target files)

cargo check

Add dependency package

cargo add serde        # Need to install cargo-edit Plugin

Installcargo-edit

cargo install cargo-edit

Dependency management

Add manually in :

[dependencies]
serde = "1.0"
reqwest = { version = "0.11", features = ["json"] }

Add local crate:

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

Add Git repository dependencies:

[dependencies]
mycrate = { git = "/user/" }

Tests & Documentation

test

cargo test

Generate a document

cargo doc --open

Post Crate to

cargo login                # Login (token required)cargo publish              # Publishcargo package              # Pack and check

Build configuration and workspace

If there are multiplecrateThe project consists of a project:

Root directoryConfiguration:

[workspace]
members = [
    "core",
    "utils",
    "web"
]

Commonly used cargo plugins

cargo install cargo-edit         #Manage dependencies (cargo add/remove/etc)cargo install cargo-watch        # Automatic monitoring and recompilationcargo install cargo-audit        # Audit security issuescargo install cargo-outdated     # Check if the dependency expires

Command quick lookup table

  • cargo new: Create a project
  • cargo build: Compile the project
  • cargo run: Compile and run
  • cargo check: Check whether the code is compileable
  • cargo test: Run the test
  • cargo doc --open: Generate and open the document
  • cargo add xxx: Add dependencies (requires plug-in)
  • cargo update: Update dependencies to the latest version
  • cargo clean: Clean up the build products
  • cargo install: Install binarycrate(likeripgrep

This is the end of this article about the tutorial on using the Rust cargo command line tool. For more related contents of the Rust cargo command line tool, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!