A build.rs script runs just before Cargo builds the package
Answer: If a package root contains build.rs, Cargo compiles that file and runs it just before building the package. Build scripts are for work such as compiling bundled C code, finding a system library, or generating Rust source. The script talks to Cargo by printing instructions that start with cargo::.
Inputs, outputs, and reruns
Just before the package build, Cargo compiles the script if needed and then runs it. Inputs arrive as environment variables. The current directory is the package root. Save generated files in OUT_DIR. Do not modify files outside that directory. Cargo does not empty OUT_DIR between builds.
By default the script runs again when any file in the package changes. The rerun-if commands narrow what should trigger another run. Exit with a non-zero code to halt the build on error. Each stdout line that starts with cargo:: is an instruction to Cargo; every other line is ignored.
See also
Claims
- id: C1 claim: A build.rs file in the package root is compiled and run just before the package is built. source: S1 evidence: "execute it just before building the package"
- id: C2 claim: The script communicates with Cargo by printing cargo:: instructions. source: S1 evidence: "printing specially formatted commands prefixed with cargo::"
- id: C3 claim: Build script outputs should go in the OUT_DIR directory. source: S2 evidence: "directory specified in the [
OUT_DIRenvironment variable]"
- id: C4 claim: Cargo does not clean OUT_DIR between builds. source: S2 evidence: "Cargo does not clean or reset
OUT_DIRbetween builds"
Sources
- [S1] Build Scripts. The Cargo Book. https://doc.rust-lang.org/cargo/reference/build-scripts.html . License: MIT OR Apache-2.0. Accessed 2026-11-17.
- [S2] build-scripts.md (The Cargo Book source). rust-lang/cargo (GitHub). https://github.com/rust-lang/cargo/blob/master/doc/book/src/reference/build-scripts.md . License: MIT OR Apache-2.0. Accessed 2026-11-17.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Cargo Book, Build Scripts, MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers; rust-lang/cargo (GitHub), build-scripts.md (The Cargo Book source), MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Cargo Book, Build Scripts, MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers; rust-lang/cargo (GitHub), build-scripts.md (The Cargo Book source), MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers.
Cite this
iinventedlight. 2026. "A build.rs script runs just before Cargo builds the package." iinventedlight Library, iil:note:m6mcdbaz, version of 2026-11-17. https://iinventedlight.com/library/notes/rust-cargo-build-scripts/. License: CC-BY-4.0.
@misc{iil_note_m6mcdbaz,
author = {{iinventedlight}},
title = {{A build.rs script runs just before Cargo builds the package}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-17},
url = {https://iinventedlight.com/library/notes/rust-cargo-build-scripts/},
note = {Permanent ID iil:note:m6mcdbaz. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-cargo-build-scripts.md.