cargo new my-axum-app

Learn how to create a new Axum project directory with cargo new, understand what the command creates, and explore the options that matter most for your first Axum application.

Before you can write a single line of Axum code, you need a project folder. In the Rust world, cargo handles that job with a single command. This page covers everything a beginner needs to know about cargo new my-axum-app: what it does, what it creates, the different ways to run it, and the mistakes that often trip people up.

What cargo is and why you use it here

If you have already installed Rust, you also have cargo. Cargo is Rust's official package manager and build tool. It does three main jobs for you:

  1. It creates new Rust projects with a standard structure.
  2. It downloads and manages libraries (called crates) that your project depends on.
  3. It compiles your code and runs your program.

For an Axum application, you will use cargo constantly. The first thing you do is tell cargo to create a fresh project folder with the exact name you want. That folder becomes the home for all your Axum code.

Think of cargo new like creating a new empty notebook with pre-printed page numbers, a table of contents, and your name on the cover. You could draw those lines yourself, but why bother when cargo can do it perfectly every time?

The simplest command

Open a terminal or command prompt on your computer. Navigate to the folder where you want to keep your programming projects. Then type:

cargo new my-axum-app

Press Enter. Cargo will do a few things very quickly:

  • Create a new folder named my-axum-app in your current location.
  • Create a file named Cargo.toml inside that folder. This file describes your project and its dependencies.
  • Create a src folder inside my-axum-app.
  • Create a file named main.rs inside src. This file contains a tiny Rust program.
  • If Git is installed, initialize a new Git repository inside the folder and add a .gitignore file.

You will see output similar to this:

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

The word binary in that output means your project is set up to produce an executable program, not a library that other programs use. An Axum web server is an executable, so a binary project is exactly what you need.

Everything worked:

If you saw the "Creating binary" message and no error, the command succeeded. You now have a valid Rust project folder on your disk.

What actually gets created on disk

After running the command, open your file explorer or use ls (or dir on Windows) inside the new folder. You will see this structure:

my-axum-app/
├── .git/
├── .gitignore
├── Cargo.toml
└── src/
    └── main.rs

Let's briefly explain each piece without diving too deep into any single file. Knowing that they exist right now helps you understand what cargo new gave you.

  • Cargo.toml - The manifest file. It stores the project name, version, Rust edition, and the list of dependencies. When you later add Axum, Tokio, and other crates, their names and versions go here.
  • src/main.rs - The entry point of your binary. For a binary project, Rust looks for a function named main inside this file. Cargo fills it with a small "Hello, world!" program so you can run it immediately.
  • .gitignore - A file that tells Git which files and folders to ignore. Cargo creates a sensible default so you do not accidentally commit build artifacts.
  • .git/ - The hidden folder that stores your Git repository data. This only appears if Git is installed and you did not disable version control.

A common beginner question: "Why did it create a src folder?" Rust projects separate source code from configuration and build output. All .rs files live under src/. That convention is consistent across every Rust project you will ever encounter.

The --bin flag and why it appears in many tutorials

Search the internet for "create an Axum project" and you will often see:

cargo new my-axum-app --bin

The --bin flag explicitly tells cargo to create a binary project. Cargo has always defaulted to a binary project - --bin is the default behaviour, and you pass --lib only when you want a library instead. Many tutorials still include --bin for explicitness, not out of necessity. cargo new my-axum-app and cargo new my-axum-app --bin produce the exact same result.

Why mention this? Because you will encounter both forms in documentation, blog posts, and videos. Understanding that --bin is now optional but harmless will save you from confusion.

You can verify the project type by looking at the Cargo.toml file. A binary project has no [lib] section and the source file is src/main.rs. A library project would have src/lib.rs and no main.rs. For Axum, you need the binary.

Explicit is fine:

Using --bin even though it's the default is perfectly acceptable. It communicates your intent clearly to other developers who read your commands. Choose whichever form you prefer.

Creating the project with a different name

my-axum-app is just a name. You can use any valid crate name. Cargo enforces a few naming rules:

  • Prefer lowercase letters, numbers, and hyphens.
  • The name cannot start or end with a hyphen.
  • The name cannot contain spaces.
  • Underscores are allowed locally and Cargo accepts them silently (they are valid snake_case), but crates.io package names should use hyphens.
  • The name cannot be a Rust keyword like match or loop.

Valid examples:

cargo new axum-server
cargo new my-first-axum-project
cargo new backend-v2

Examples to avoid (errors or warnings):

cargo new MyAxumApp     # Works, but Cargo warns; prefer myaxumapp
cargo new axum_app      # Works with no warning; prefer axum-app for crates.io
cargo new my axum app   # Fails because spaces are not allowed
cargo new 123app        # Fails because it starts with a number
cargo new axum-app!     # Fails because exclamation mark is not allowed

cargo new axum_app still creates a folder named axum_app and keeps the package name as axum_app in Cargo.toml (it does not rewrite underscores to hyphens). Cargo warns only about names that are neither snake_case nor kebab-case - for example, MyAxumApp produces a warning suggesting myaxumapp - while underscore names are accepted silently. crates.io treats _ and - as the same normalized name and prefers hyphens. For local Axum apps, use hyphens from the start so the folder name, package name, and crates.io rules stay aligned.

Stick with lowercase letters and hyphens. my-axum-app is a perfect name.

Adding version control from the start

By default, cargo new runs git init in the new folder if Git is installed and the folder is not already inside a Git repository. This sets up version control automatically, which is a good habit for any project.

If you do not want Git initialized, you can disable it with the --vcs none flag:

cargo new my-axum-app --vcs none

This creates the same files but skips the .git folder and .gitignore. You might use this if you plan to manage version control differently, or if you are creating many small test projects and do not want dozens of Git repositories.

If you want to force Git even when it would otherwise be skipped, use --vcs git:

cargo new my-axum-app --vcs git

For most beginners, the default behavior is perfect. Keep Git tracking your progress from the very first command.

Choosing a Rust edition

Rust occasionally releases new "editions" that bundle language improvements and breaking changes. Edition 2024 is stable, and modern cargo new defaults to the latest stable edition (typically edition = "2024" in the generated Cargo.toml). You usually do not need to specify anything.

If you want to pin a specific edition, use the --edition flag:

cargo new my-axum-app --edition 2021

For an Axum starter project, the default is the best choice. Axum 0.8 works with a recent Rust toolchain and any edition 2018 or later. You will not need to touch this flag unless you have a specific reason.

What if the directory already exists?

If you run cargo new my-axum-app and a folder with that name already exists, cargo will refuse to overwrite it. You will see an error like:

error: destination `/home/user/projects/my-axum-app` already exists

This is a safety feature. Cargo will never delete or overwrite files you already have. If you see this error, you have three options:

  1. Choose a different project name.
  2. Delete the existing folder manually (make sure you don't need anything inside).
  3. Move the existing folder to a different location.

There is no --force flag for cargo new - if you try it, Cargo rejects the command with error: unexpected argument '--force' found. The destination-exists check cannot be overridden; pick a different name or move the existing folder first.

Check before you delete:

Deleting a folder is permanent. Before you remove an existing directory to make room for cargo new, check its contents. You might have valuable work inside.

Creating the project step by step

The process of setting up a fresh Axum project with cargo new has a natural order. Here is exactly what you do from a blank terminal:

1

Step 1: Open a terminal and choose a parent directory

Open your terminal application. On Windows, that might be PowerShell or Command Prompt. On macOS or Linux, it's usually Terminal.

Navigate to the folder where you want to keep all your programming projects. For example, if you want a projects folder in your home directory, run:

cd ~
mkdir projects
cd projects

This step is about being in the right place before creating the new project. Cargo creates the project folder inside your current working directory.

2

Step 2: Run the cargo new command

Inside your chosen parent folder, execute:

cargo new my-axum-app

You will see the "Creating binary" message. The command completes almost instantly.

3

Step 3: Verify the folder was created

List the contents of your current directory:

ls

You should see a new entry named my-axum-app. If you do not, double-check that you are in the right place and that the command did not print an error.

4

Step 4: Enter the project folder

Move into the new directory:

cd my-axum-app

From now on, all commands for this project (like adding dependencies and running the server) happen inside this folder. The terminal prompt usually changes to show the folder name.

This sequence is the foundation for the whole setup. Each step depends on the previous one, and skipping any of them will cause later commands to fail.

Using cargo init as an alternative

There is a related command called cargo init. It serves the same purpose as cargo new but with a small difference: it initializes a project in the current directory instead of creating a new folder.

mkdir my-axum-app
cd my-axum-app
cargo init

After these three commands, the current directory contains the same Cargo.toml, src/main.rs, and Git setup as if you had run cargo new my-axum-app from the parent folder. The result is identical.

When would you use cargo init? You might use it if you already created an empty folder and want to turn it into a Rust project without changing the folder name. But for a beginner, cargo new is more common and less error-prone because it handles the folder creation for you.

Which one should you use for your Axum project? Either works. For your Axum project, cargo new is the simpler and more common choice, so that is what we recommend following.

What happens if you run cargo new inside an existing Git repository?

By default, cargo new will not initialize a new Git repository if the current directory is already part of a Git repository. Instead, it just creates the project folder and leaves the outer Git repository to track it. This behavior can be surprising if you expected an inner .git folder.

For example, if you have a learning-rust folder that is already a Git repo, and inside it you run:

cargo new my-axum-app

The new my-axum-app folder will not get its own .git directory, and Cargo will not even create a .gitignore file for it. Git will treat it as part of the outer repository. That's usually fine, but if you want an independent Git repository for each Axum project, navigate to a folder that is not inside any Git repo first.

You can always force Git initialization with --vcs git if you want the inner repo anyway.

Common mistakes beginners make with cargo new

Several errors appear frequently when people first use this command. Let's name them so you can recognize and fix them quickly.

Mistake 1: Running the command from the wrong directory

You might run cargo new my-axum-app and then wonder why you cannot find the folder. The reason is usually that your terminal was in a different directory than you thought. The pwd command (print working directory) shows exactly where you are. Always check with pwd before creating a project, especially if you use multiple terminal tabs.

Mistake 2: Using a name that violates cargo's rules

As mentioned earlier, uppercase letters trigger a warning, while spaces and special characters are not allowed. The error message from cargo is usually clear, but if you are not used to reading compiler output, it can look intimidating. Read the first line of the error; it almost always states the problem plainly.

Mistake 3: Forgetting to cd into the new folder

After cargo new my-axum-app, your terminal is still in the parent folder. Many beginners then try to run cargo run and get an error like "could not find Cargo.toml". That happens because cargo run must be executed from inside the project folder. You need to run cd my-axum-app first. Just remember: after creating, always change into the folder.

This mistake breaks your workflow:

Running cargo run without being inside the project folder will always fail with "could not find Cargo.toml". The fix is one command: cd my-axum-app.

Mistake 4: Using cargo new when you meant cargo init

Some users create a folder manually, enter it, and then run cargo new my-axum-app expecting it to initialize the current folder. Instead, cargo creates a nested folder with that name. If you are already inside the folder you want to use, the correct command is cargo init without a name.

Mistake 5: Not having Cargo installed

If you see an error like command not found: cargo, it means Cargo is not installed or not in your terminal's PATH. This is an environment setup issue, not a problem with cargo new itself. You need to install Rust properly so that cargo is on your terminal's PATH.

The complete command with all common flags together

For a new Axum project, you rarely need more than one or two flags. The most complete form you might see in a professional workflow looks like this:

cargo new my-axum-app --bin --edition 2024 --vcs git

Broken down:

  • --bin forces a binary crate (redundant but explicit).
  • --edition 2024 pins the Rust edition (2024 is also the current default).
  • --vcs git forces Git initialization.

But for a beginner, running the simple version is perfectly fine:

cargo new my-axum-app

Everything else uses sensible defaults. Do not feel like you need to memorize all flags before starting. You can always add them later if needed.

What you should do right after cargo new

The very next action is to enter the project folder. Once inside, you can verify the project compiles by running:

cargo run

That command will compile the default "Hello, world!" program and run it. You should see output like:

   Compiling my-axum-app v0.1.0 (/path/to/my-axum-app)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/my-axum-app`
Hello, world!

This confirms that your Rust toolchain works and that the project was created correctly. After that, you are ready to start adding Axum dependencies and writing your web server code.

One key insight about project setup

The cargo new command seems trivial - it just makes a folder and a couple of files. But those files follow a strict convention that the entire Rust ecosystem expects. When you later run cargo build, cargo test, or cargo run, Cargo looks for Cargo.toml and src/main.rs in exactly the places where cargo new puts them. If those files are missing or in the wrong location, nothing else works.

By using cargo new instead of manually creating files, you guarantee that your project starts with the correct structure. That small guarantee saves hours of debugging later, especially when you add dependencies like Axum and Tokio.

Summary

You now know how to create an Axum project folder with cargo new my-axum-app, what the command creates, how to customize it with flags, and what errors to watch for. The most important habit to form right now is: always run cargo new from the correct parent directory, and always cd into the new folder immediately afterward. That two-step routine will carry you through every Rust project you ever build.

The next logical step after creating this project is to explore the generated Cargo.toml and understand what a binary crate looks like. From there, you will add the Axum dependency and turn this empty shell into a running web server.