Sub topic 5
Creating your first project with cargo new
This chapter assumes you have already installed Rust with rustup and confirmed that rustc --version and cargo --version print version strings. You should be comfortable running commands in a terminal. Here you create a project, run it, and learn how to pick between a binary and a library.
What cargo new does
cargo new creates a new directory, fills it with a minimal project, and initializes a Git repository in it (unless you are already inside one). A Cargo project is called a package. A package contains one or more crates, and a crate is a unit of compilation. A crate is either a binary, which produces an executable, or a library, which other code can use.
By default, cargo new creates a binary package.
Create and run a project
Run these commands from a directory where you keep your projects:
cargo new hello_cargo
cd hello_cargo
cargo runcargo run compiles the project and then runs the program. The first run prints some compiler progress lines, then the program's output:
Compiling hello_cargo v0.1.0 (/home/you/hello_cargo)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.4s
Running `target/debug/hello_cargo`
Hello, world!The exact wording of the progress lines varies between Cargo versions.
The generated source file
Cargo writes a working program to src/main.rs:
fn main() {
println!("Hello, world!");
}main is the entry point. When you run the executable, execution begins in this function. println! is a macro (the ! marks it as one) that prints a line of text.
The manifest
Cargo also writes Cargo.toml in the project root:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]The [package] table holds the project's name, version, and Rust edition. The [dependencies] table starts out empty. The edition value that cargo new writes depends on your Cargo version, so you may see a newer one. Later chapters cover this file in detail.
Binary or library
Pass --lib to create a library instead:
cargo new my_library --libA library has src/lib.rs instead of src/main.rs, and it has no main function. You cannot cargo run a library, but you can build and test it.
| Command | Entry file | Produces |
|---|---|---|
| `cargo new name` | `src/main.rs` | An executable program |
| `cargo new name --lib` | `src/lib.rs` | A library that other crates can depend on |
Use a binary when you are building something you run. Use a library when you are building code to be shared or reused.
How the pieces connect
Common errors
destination already exists:cargo newrefuses to write into a directory that already exists and has content. Choose another name, or usecargo initfrom inside an existing directory.- Invalid package name: Names may contain letters, digits,
-, and_, and must not start with a digit. Cargo also rejects names that clash with Rust keywords. cargo: command not found: Cargo'sbindirectory is not on yourPATH. Open a new terminal, or revisit the installation steps.
Try it yourself
Exercise
You run cargo new text_tools --lib. Which source file does Cargo create, and why does cargo run fail in this project?
Show answer
Cargo creates src/lib.rs. A library crate has no main function, so there is no executable entry point to run. You can still use cargo build and cargo test.
Exercise
After cargo new hello_cargo, you edit the message in src/main.rs to print Hello, Cargo!. Which command both rebuilds and runs the program, and which directory holds the resulting debug binary?
Show answer
cargo run rebuilds the changed code and runs it. The debug binary is in target/debug/, at target/debug/hello_cargo.