What are Cargo and Cargo.toml in Rust?
Created Jan 25, 2024Last modified Jan 25, 2024
Rust Basics
- [1] How to install Rust?Jun 2, 2024
- [2] What are Cargo and Cargo.toml in Rust?Jan 25, 2024
Cargo
cargo
is the command-line tool that comes with Rust. It is used for various tasks related to managing Rust projects.
Cargo handles tasks like building the project, downloading and managing dependencies, running tests, and creating documentation.
common commands
- cargo new: Creates a new Rust project.
- cargo build: Builds the project.
- cargo run: Builds and runs the project.
- cargo test: Runs tests in the project.
- cargo doc: Generates documentation.
- cargo publish: Publishes a crate to the Rust package registry (crates.io).
Example workflow:
# Create a new project
cargo new my_project
# Navigate to the project directory
cd my_project
# Build and run the project
cargo run
# Run tests
cargo test
# Initialize in an existing folder
cargo init
Cargo simplifies many aspects of Rust development, making it easy to get started and manage projects, dependencies, and builds.
Cargo.toml
Cargo.toml
is the manifest file for a Rust project. It contains metadata about the project, such as its name, version, dependencies, and other settings.
The file is written in TOML (Tom's Obvious, Minimal Language), which is a configuration file format that's easy to read due to its simple syntax.
Example Cargo.toml
file:
[package]
name = "my_project"
version = "0.1.0"
authors = ["Your Name <your@email.com>"]
edition = "2021"
[dependencies]
rand = "0.8.5"