Page Content

Tutorials

What is the use of Cargo in Rust?

Introduction to Cargo

Cargo in Rust is a build system and package manager. It is the standard tool used in the Rust network to build and run applications. While you can compile simple programs directly with the Rust compiler, rustc, most Rust developers use Cargo because it handles many tasks for you.

You can see here for Rust Installation procedure

Cargo’s responsibilities:

  • Building your code.
  • Downloading libraries your code needs (which are called dependencies or crates in Rust).
  • Building those dependencies.
  • Running tests.
  • Generating documentation.
  • Managing packages.

A Rust project managed by Cargo is called a package. A package contains a Cargo.toml file and one or more crates. A crate is the smallest amount of code the compiler considers, and it can be a library or an executable binary.

Creating Your First Project with Cargo

start a new Rust project is by using the cargo new command. This command creates a new directory for your project and sets up a standard project structure.

Let’s create a project named hello_cargo:

  1. Open your terminal or command prompt.
  2. Navigate to a directory where you want to store your projects.
  3. Run the cargo new command followed by your desired project name. We’ll also add –bin to explicitly create a binary executable project, although this is the default.

For mac OS or Linux

$ cargo new hello_cargo 

For Windows

cargo new hello_cargo 

Navigate into the newly created project directory.

$ cd hello_cargo
    cd hello_cargo

    Understanding the Project Structure and Code

    The new cargo command has created a standard directory structure for you. If you list the files in the hello_cargo directory, you’ll see:

    .
    ├── Cargo.toml
    └── src
        └── main.rs

    .gitignore: Cargo also initialises a Git repository by default unless you are already in one, creating .git and .gitignore files. You can change this behavior with the –vcs flag.

    Cargo.toml: This is the manifest and configuration file for your project, written in the TOML format. It contains important details about your package, such as its name, version, and dependencies.

    Open Cargo.toml in a text editor. It should look something like this:

    [package]
    name = "hello"
    version = "0.1.0"
    edition = "2024"
    [dependencies]

    The [package] section holds metadata. The [dependencies] section is where you would list any external libraries (crates) your project uses.

    src/main.rs: Cargo expects your source files to be located in the src directory. For a binary crate (an executable application), src/main.rs is the default crate root and contains the main entry point of your program.

    Open src/main.rs. Cargo has automatically generated the “Hello, world!” program for you:

    fn main() {
        println!("Hello, world!");
    }

    You could see here above architecture details.

    Building and Running with Cargo

    Instead of using rustc directly, you’ll use Cargo commands to build and run your project.

    1. Make sure you are still in the hello_cargo project directory.
    2. To build your project, run the command ‘cargo build’.
    $ cargo build
    cargo build

    Cargo will compile your code and its dependencies (none for this simple project) and place the executable in the target/debug directory.

    after run command output see like this, The first lines show Cargo compiling and building,

    Finished dev [unoptimized + debuginfo] target(s) in ... secs
         Running `target/debug/hello_cargo`

    To build and run your project in a single step, use cargo run.

    $ cargo run
    cargo run

    This command will build the project if it hasn’t been built or if changes have been made, and then run the resulting executable.

    You should see the following output :

    Hello, world!

    the output of your program.

    Using Cargo is the convention for Rust projects and is highly recommended for anything beyond single-file examples, as it greatly simplifies project management, especially when dealing with dependencies.

    You ca also Read What is Rust programming used for?

    Index