← All notes

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

Sources

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

Permanent ID: iil:note:m6mcdbaz

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.},
}

Markdown

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.