Release target directory

Learn what the target release directory contains, where the finished Axum binary lives, and how Cargo organises build output, caches, and temporary files

The release target directory is the folder Cargo fills when a release build runs. It lives at target/release inside the project, and it holds the finished binary plus everything Cargo needed to produce it: compiled dependencies, build-script output, incremental compilation data, and small bookkeeping files. Knowing what is inside it turns the folder from a mysterious pile of files into a map a beginner can read.

Where This Fits

The previous page ran cargo build --release and pointed at one file, target/release/axum-release-demo. That file is the most important thing in the folder, but it is far from the only thing. A real project leaves hundreds of files behind in target/release, and a beginner who opens the folder for the first time usually wonders which of them matter, which can be deleted, and which should never be touched. This page answers those questions by walking through the folder as it actually appears after a build.

Think of the folder like a kitchen after a meal is cooked. The finished dish sits on the counter, ready to serve. Around it are the mixing bowls, the chopping boards, and the recipe notes that were used to make it. You need the dish. You do not need to wash the bowls by hand every time, but you do need to know which ones are safe to put away and which ones the cook still needs for the next meal.

The Smallest Working Version First

Start with the same small project from the previous page, build it in release mode, and list the folder:

Cargo.toml
[package]
name = "axum-release-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.8"
tokio = { version = "1", features = ["full"] }
src/main.rs
use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, release world!" }));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("Server running at http://127.0.0.1:3000");
    axum::serve(listener, app).await.unwrap();
}
cargo build --release
ls target/release/

On the machine used to check this page, with Cargo 1.100.0, the listing looks like this:

axum-release-demo
axum-release-demo.d
build
examples
incremental

Five entries only, at the top level. The binary is the first line. The rest of the work is tucked away inside the three folders. That is the whole top level for a simple binary project, and it is worth learning to read before opening any of the folders.

You Are Looking at the Right Folder:

If ls target/release/ shows a file with the same name as the project, the release build finished and the binary is there. That one check is enough to confirm the build output landed where it should.

The Binary and Its Companion File

The first two entries belong together. axum-release-demo is the finished executable, the file that runs the server. The name comes from the name field in the [package] section of Cargo.toml, so renaming the package renames the binary on the next build. The file is executable, which means the operating system can run it directly with ./target/release/axum-release-demo, with no Cargo involved.

The second entry, axum-release-demo.d, is a small text file called a dep-info file. It lists the source files the binary was built from, which for this project is just one line:

/home/hatch/workspace/axum-ch22-scratch-build/target/release/axum-ch22-scratch: /home/hatch/workspace/axum-ch22-scratch-build/src/main.rs

Cargo reads that file on the next build to decide whether anything changed. If src/main.rs has a newer timestamp than the binary, Cargo rebuilds. If nothing changed, Cargo skips the work and prints Finished at once. The .d file is how Cargo knows the binary is fresh. It is not a file a person ever needs to open or edit, but understanding its job explains why a second build finishes in a fraction of a second.

Never Edit Files Inside target:

Every file under target is generated by Cargo, and Cargo assumes it owns them. Editing a .d file, a binary, or anything inside build by hand will either be overwritten on the next build or confuse Cargo into rebuilding everything. Treat the whole folder as read-only output, and make changes in src or Cargo.toml instead.

The build Folder

The build folder holds the output of build scripts and the compiled form of every dependency crate. Its layout on the machine used to check this page looks like this:

target/release/build/
    axum/
    axum-core/
    bytes/
    hyper/
    hyper-util/
    libc/
    tokio/
    ...

Each folder inside build is named after one crate the project depends on, directly or indirectly. Opening one, for example target/release/build/tokio/, shows a folder named with a short hash and two folders inside it:

target/release/build/tokio/58d2acfd26366184/
    fingerprint/
    out/

The out folder holds the compiled library for that crate, a file ending in .rlib, plus the output of the crate's build script, if it has one. A build script is a small Rust program a crate can ship that runs before the crate itself is compiled, usually to detect something about the machine or to generate a little code. The fingerprint folder holds small files Cargo uses to decide whether that crate needs to be rebuilt. The hash in the folder name keeps builds of the same crate with different settings apart, so a debug build and a release build never share each other's compiled crates.

A beginner does not need to open these folders during normal work. Their value is in what they explain: when a release build takes two minutes the first time, most of that time is spent compiling these crates, one folder at a time, and Cargo keeps the results here so the next build can skip them.

The incremental Folder

The incremental folder holds data for incremental compilation, the feature that lets the compiler reuse parts of a previous build. The release profile has incremental compilation switched off by default, so for a plain cargo build --release this folder stays small and holds little of value. It exists because Cargo creates the folder structure for every profile, and because a project can turn incremental compilation back on for release builds if it wants to.

The folder matters more in target/debug, where incremental compilation is on and the folder grows with every rebuild. In the release folder, its presence is mostly a reminder that the two profiles keep separate caches: nothing in target/release/incremental is shared with target/debug/incremental, and deleting one never affects the other.

The examples Folder

The examples folder appears when a project has example programs, which are small runnable programs kept in an examples folder at the project root. Each example is compiled into its own binary and placed in target/release/examples. A project with no examples still gets an empty examples folder in the release directory, because Cargo prepares the space in advance. Example binaries are built with the same profile as the rest of the build, so an example built with --release is an optimised example binary.

The Whole target Folder

One level up, the target folder itself holds more than the release folder. After building the same project in both modes, the listing on the machine used to check this page was:

target/
    CACHEDIR.TAG
    .rustc_info.json
    debug/
    release/

The debug folder mirrors the release folder exactly: its own binary, its own .d file, its own build, examples, and incremental folders, all compiled with debug settings. The two profiles never mix their output. That separation is what makes it safe to switch between cargo build and cargo build --release freely: each command reads and writes only its own folder.

CACHEDIR.TAG is a marker file that tells backup and caching tools that the folder holds data that can be regenerated, so it is safe to skip when copying a project. .rustc_info.json records information about the compiler version that produced the builds, which Cargo uses to decide whether a new compiler version means everything needs to be rebuilt.

How Big the Folder Gets

Build output is large compared with the source code. On the machine used to check this page, the same one-route Axum server, built with axum 0.8.9 and tokio 1.54.1 under Rust 1.100.0, used this much disk space:

FolderSize
target/debug250 MB
target/release127 MB
target total377 MB

The source code for that project is a few kilobytes. The rest is compiled dependencies, most of it Tokio, Hyper, and the crates they pull in. The debug folder is bigger because it keeps debug information for every crate. The release folder is smaller for the same reason the release binary is smaller: no debug information, and optimised code.

Those numbers belong to one small project on one machine, and they grow as dependencies are added. A project with a database driver, a serialisation library, and a few more crates can easily pass a gigabyte in target. That is normal, and it is the main reason the folder is never committed to version control.

Sizes Vary by Project:

The sizes above were measured on one machine for one small project. Treat them as an example of the scale, not as numbers to expect. Measure your own project with du -sh target/debug target/release to see your own numbers.

Keeping target Out of Version Control

The target folder is generated output, and generated output does not belong in a Git repository. It is large, it changes on every build, and any other developer can regenerate it by running cargo build. Committing it would make every clone download hundreds of megabytes of compiled files that the new machine would rebuild anyway.

A new Rust project created with cargo new already contains a .gitignore file with one line:

/target

That line tells Git to ignore the folder. If a project was created some other way and has no .gitignore, add that line by hand. The leading slash matters: it means the target folder at the project root, not a folder named target anywhere deeper in the tree. After adding the line, git status stops listing build output, and the repository stays small.

Check Before the First Commit:

If git status shows hundreds of files under target/, the .gitignore entry is missing or was added after the files were already tracked. Adding the line afterwards does not untrack files Git already knows about. Fix it before the first commit, while the repository is still clean, rather than trying to remove a committed target folder later.

Cleaning the Folder

When the folder grows too large, or when a build starts behaving strangely after a toolchain update, Cargo can remove the generated files:

cargo clean

That command deletes the whole target folder. The next build recompiles everything from scratch, so it takes as long as the first build did. The command is safe: it only removes files Cargo generated, and the source code, Cargo.toml, and Cargo.lock are untouched.

Two narrower forms exist for when deleting everything is more than needed. cargo clean -p axum-release-demo removes only the artifacts for one package, leaving the compiled dependencies in place, so the next build only recompiles that package. cargo clean --release removes only the release output and leaves the debug output alone. The dry-run flag shows what would be deleted without deleting anything:

cargo clean -n

That prints the paths Cargo would remove. It is a useful check before running the real command on a machine where disk space is tight and the next full rebuild would be expensive.

Moving the Folder Somewhere Else

Sometimes the default location is wrong. A project on a small disk, or a project shared between machines, may need the build output somewhere else. Cargo accepts a different location in two ways. The first is an environment variable, set before the build command:

CARGO_TARGET_DIR=/mnt/fast-disk/build-output cargo build --release

The second is a flag on the build command itself:

cargo build --release --target-dir /mnt/fast-disk/build-output

Both do the same thing: the target folder is created at the given path instead of inside the project, and the release binary lands at /mnt/fast-disk/build-output/release/axum-release-demo. Everything else about the build is unchanged. The flag form is easier to remember for a one-off build, and the environment variable form is easier when every build in a session should use the same location.

One detail catches beginners out: the setting is per build, not per project. Running one build with --target-dir and the next build without it produces two separate target folders, and the second build recompiles everything because it cannot see the first build's cache. Pick one location and use it consistently, or accept the cost of rebuilding when switching.

How Cargo Decides What to Rebuild

The whole folder exists to make rebuilds fast, and the mechanism is simple enough to describe in a few lines. For each crate, Cargo records the fingerprint of everything that went into compiling it: the source files, the compiler version, the profile settings, and the features that were enabled. On the next build, Cargo compares the current fingerprint with the stored one. If they match, the compiled crate is reused. If anything differs, the crate is recompiled, along with every crate that depends on it.

That is why changing one line in src/main.rs recompiles only the project crate, while changing a feature flag in Cargo.toml can recompile half the dependency tree. The feature flag changes the fingerprint of Tokio, and every crate that depends on Tokio follows. It is also why switching compiler versions triggers a full rebuild: the compiler version is part of every fingerprint, so a new compiler invalidates all of them at once.

src/main.rs changed
        |
        v
fingerprint of axum-release-demo no longer matches
        |
        v
only axum-release-demo is recompiled
        |
        v
dependencies are reused from target/release/build
        |
        v
linker joins the fresh crate with the reused ones

The diagram shows the common case. The reverse case, changing something deep in the tree, flows the other way: the changed crate is recompiled, and everything above it follows, because each crate above it now links against a different dependency.

What Belongs in the Folder and What Does Not

A short rule covers almost every question about the folder. If Cargo put it there, leave it there and let Cargo manage it. If a person put it there, it probably does not belong. The binary is the one exception that proves the rule: Cargo put it there, and copying it somewhere else is the entire point of a release build. Everything else in the folder is either a cache Cargo needs or a byproduct Cargo will regenerate.

Three practical checks follow from that rule. The binary can be copied, moved, renamed, and run from anywhere, because it is a finished program. The .d files, the build folder, and the incremental folder can be deleted, but only by cargo clean, because deleting them by hand can leave fingerprints pointing at files that no longer exist. And the folder as a whole can be deleted at any time with cargo clean, because everything in it can be rebuilt from the source code and Cargo.lock.

A Richer Complete Example

A project with a library, a binary, and an example shows the folder at its fullest. The layout below is a small project that keeps its router in src/lib.rs, its startup code in src/main.rs, and a demo program in examples/demo.rs:

src/lib.rs
use axum::{routing::get, Router};

pub fn app() -> Router {
    Router::new().route("/", get(|| async { "Hello, release world!" }))
}
src/main.rs
use axum_release_demo::app;

#[tokio::main]
async fn main() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("Server running at http://127.0.0.1:3000");
    axum::serve(listener, app()).await.unwrap();
}
examples/demo.rs
fn main() {
    println!("This example prints without starting a server.");
}

Build everything in release mode:

cargo build --release --examples

The release folder now holds two binaries instead of one:

target/release/axum-release-demo
target/release/examples/demo

The main binary sits at the top level, as before, and the example binary sits inside the examples folder. Both are optimised release binaries. The example is run with its path, just like the server:

./target/release/examples/demo

The output is the single line from the example:

This example prints without starting a server.

The same rule about freshness applies to both binaries. Editing examples/demo.rs and rebuilding recompiles only the example. Editing src/lib.rs recompiles the library, the binary, and the example, because both of them depend on the library.

Common Beginner Mistakes

Looking for the binary in the project root

A beginner sometimes runs cargo build --release and then types ./axum-release-demo, expecting the binary to appear next to Cargo.toml. It never does. Cargo always puts build output under target, in the folder for the profile that was used. The correct path is ./target/release/axum-release-demo, with both folder names in it.

Running the debug binary by mistake

The two folders sit side by side, and their binaries have the same name. Typing ./target/debug/axum-release-demo runs the debug build, which works fine and looks identical on the surface. The mistake only shows up later, in the file size and the speed. When a release build is what was wanted, check that the path says release before running it.

Deleting files inside target by hand

Deleting a single folder inside target/release/build to save space feels harmless, but Cargo's fingerprints still point at the deleted files. The next build can fail with a confusing error about a missing file, or silently rebuild far more than expected. Use cargo clean or cargo clean -p <name> instead, because those commands remove the fingerprints together with the files they describe.

Committing target to Git

A first commit that includes target adds hundreds of megabytes to the repository and slows every future clone. The fix is the one-line .gitignore entry shown above, added before the first commit. If the folder is already committed, removing it from tracking is a separate cleanup job, and it is much easier to avoid the problem than to repair it.

Assuming the folder is the same on every machine

The layout shown on this page was measured with Cargo 1.100.0. Older and newer versions of Cargo arrange the inside of target differently, and the exact names of the folders under build change as dependencies change. The parts that stay stable are the ones that matter: the binary at target/release/<name>, the separation between debug and release, and the fact that everything inside is generated output. Rely on those three facts, and treat the deeper layout as detail that can change.

Final Complete Example

Everything on this page, as one short session a beginner can follow from an empty folder:

cargo build --release
ls target/release/
./target/release/axum-release-demo

The first command fills the folder. The second shows the binary, its .d file, and the three folders. The third runs the server. To see how much space the build used, and to confirm which folder is which:

du -sh target/debug target/release

To remove everything and start clean:

cargo clean

And to keep the folder out of version control, check that .gitignore contains this line:

/target

Those five commands cover the daily life of the release target directory: build it, look inside it, run what it produced, measure it, clean it, and keep it out of the repository.

Summary

The release target directory is a workshop, not a warehouse. The finished binary is the product that leaves the workshop, and everything else in the folder is either a tool or a leftover that made the product possible: compiled dependencies waiting to be reused, fingerprints that tell Cargo what changed, and incremental data kept for the next build. The folder is large because compiled code is large, and it is safe to delete because every file in it can be rebuilt from source.

The decision framework is short. Run the binary from target/release/<name> and copy that file when the program needs to go somewhere else. Leave everything else in the folder alone, and let cargo clean do the deleting when space runs short. Keep /target in .gitignore so the workshop never gets committed, and remember that the debug and release folders are separate caches, so a change to one never disturbs the other. Once those habits are in place, the folder stops being a mystery and becomes what it is: the place where the finished program waits to be picked up.