Skip to main content

How to Create a Rust Project with cargo new

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Cargo, Rust's build tool and package manager, creates new projects with one command. cargo new writes a ready-to-run program, the Cargo.toml that describes it and a Git repository around it, so the first cargo run already prints Hello, world!.

Syntax​

Syntax
cargo new <name> [--lib]
PartWhat it does
cargo newCreates a new Rust project in a new folder.
<name>The folder's name, which is also the package's name, such as hello-rust.
--libOptional. Creates a library, with src/lib.rs, instead of a program with src/main.rs.

The Example​

The example creates a program named hello-rust and runs it unchanged, to see what Cargo gives you.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. Check it with cargo --version. This guide uses Cargo 1.100.0.

Step 1: Create the Project​

Terminal
cargo new hello-rust
Output
Creating binary (application) `hello-rust` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

A "binary" package is a program you can run, as opposed to a library. The commands here are the same in zsh, bash and PowerShell.

Step 2: Look at What It Created​

The hello-rust folder holds the program and its description, inside a new Git repository with a .gitignore that keeps build output out of it.

src/main.rs is the program:

src/main.rs from cargo new: a main function that prints Hello, world! with println!

src/main.rs
fn main() {
println!("Hello, world!");
}

Cargo.toml describes the package:

Cargo.toml
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2024"

[dependencies]
SettingWhat it means
nameThe package's name, taken from the folder.
versionThe package's own version.
edition = "2024"The edition of the Rust language the code is written in. 2024 is the newest.
[dependencies]The crates the project uses. cargo add fills it in.

Step 3: Run It​

Terminal
cd hello-rust
cargo run
Output
Compiling hello-rust v0.1.0 (/Users/Shared/you/hello-rust)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.91s
Running `target/debug/hello-rust`
Hello, world!

A zsh terminal where cargo new hello-rust creates a binary package, and cargo run compiles it, finishes the dev profile and prints Hello, world!

cargo run compiled the program into target/debug/hello-rust and ran it. How to Run a Rust Program with cargo run explains its output and options.

Common Mistakes​

MistakeWhat happensDo this instead
Running cargo new in a folder that should be the projectCargo creates a new folder inside itUse cargo init to make the current folder a project
Giving a name that already exists as a folderCargo refuses to overwrite itPick a new name, or use cargo init inside that folder
Running cargo run from the parent folderCargo can't find a Cargo.tomlcd into the project first

Troubleshooting​

SymptomLikely causeFix
error: destination `…/hello-rust` already existsA folder with that name is thereChoose another name, or cargo init inside it
error: could not find `Cargo.toml` in `…` or any parent directoryYou're not inside the projectcd hello-rust

FAQ​

Can I create the project without a Git repository?​

Yes: cargo new hello-rust --vcs none. Cargo also skips Git when the new folder is inside an existing repository.

What's the difference between cargo new and cargo init?​

cargo new <name> creates a new folder. cargo init turns the folder you're already in into a project.

Conclusion​

cargo new <name> creates a Rust program with src/main.rs, a Cargo.toml for the 2024 edition and a Git repository, and cargo run inside it builds and runs it.

Additional Resources​