Hello Cargo

Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs dependencies.)

1. Creating a Project with Cargo new

$ cargo new hello_cargo
    Creating binary (application) `hello_cargo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ cd hello_cargo
$ tree
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
$ ls -al
total 4
drwxrwxr-x 3 alexlai alexlai  35 Jun 19 15:10 .
drwxr-xr-x 4 alexlai alexlai 133 Jun 19 15:10 ..
-rw-rw-r-- 1 alexlai alexlai  82 Jun 19 15:10 Cargo.toml
drwxrwxr-x 2 alexlai alexlai  21 Jun 19 15:10 src

Generally it will initialize a new Git repository along with a .gitignore file, unless it was under an existing git directory. You can override this behavior by using cargo new --vcs=git project_name.

2. Cargo.toml

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
  • This file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.
  • The first line, [package], is a section heading that indicates that the following statements are configuring a package. As we add more information to this file, we’ll add other sections.
  • The next three lines set the configuration information Cargo needs to compile your program: the name, the version, and the edition of Rust to use. We’ll talk about the edition key in Appendix E.
  • The last line, [dependencies], is the start of a section for you to list any of your project’s dependencies. In Rust, packages of code are referred to as crates. We won’t need any other crates for this project, but we will in the first project in Chapter 2, so we’ll use this dependencies section then.

~/src/main.rs was auto generated by cargo new

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

Cargo has generated a “Hello, world!” program for you and placed the code in the src directory.

Building and Running a Cargo Project

From your hello_cargo directory, build your project by entering the following command:

$ cargo build
   Compiling hello_cargo v0.1.0 (/home/alexlai/mdbook-proj/learn-rust-dumindu-madunuwan/src/installation/hello_cargo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.94s
$ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    └── debug
        ├── build
        ├── deps
        │   ├── hello_cargo-1d1434a127132538
        │   └── hello_cargo-1d1434a127132538.d
        ├── examples
        ├── hello_cargo
        ├── hello_cargo.d
        └── incremental
            └── hello_cargo-3iy1psz01zhx2
                ├── s-gx99law0wn-1mpu47x-8xdrmhemgoej4eynpe4akzbft
                │   ├── 11mbs6cb0dvdfdpw.o
                │   ├── 1lh00380n6tnl874.o
                │   ├── 1sgpzgbsddx0q1vc.o
                │   ├── 426hpberye9u6ytg.o
                │   ├── 4j9q5ckxtw0qsej1.o
                │   ├── dep-graph.bin
                │   ├── query-cache.bin
                │   ├── s50pw9t7epnr7l1.o
                │   └── work-products.bin
                └── s-gx99law0wn-1mpu47x.lock

9 directories, 18 files
  • run it
$ ./target/debug/hello_cargo
Hello, world!
  • use cargo run
$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/hello_cargo`
Hello, world!
  • or, cargo check
$ cargo check
    Checking hello_cargo v0.1.0 (/home/alexlai/mdbook-proj/learn-rust-dumindu-madunuwan/src/installation/hello_cargo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

Summary

  • We can create a project using cargo new.
  • We can build a project using cargo build.
  • We can build and run a project in one step using cargo run.
  • We can build a project without producing a binary to check for errors using cargo check.
  • Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.

Building for Release

When your project is finally ready for release, you can use cargo build --release to compile it with optimizations. This command will create an executable in target/release instead of target/debug.

For more information about Cargo, check out The Cargo Book.