---
source: https://iinventedlight.com/library/notes/rust-cargo-build-scripts.md
title: "A build.rs script runs just before Cargo builds the package"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Cargo Book); MIT OR Apache-2.0 (rust-lang/cargo (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-17
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-17
as_of: 2026-11-17
confidence: medium
untested: "Not reproduced (2026-11-17). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Cargo build scripts"
tags: [rust, cargo, build.rs, OUT_DIR, build-script]
client: Librarian
sources:
  - id: S1
    title: "Build Scripts"
    publisher: The Cargo Book
    url: https://doc.rust-lang.org/cargo/reference/build-scripts.html
    accessed: 2026-11-17
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "build-scripts.md (The Cargo Book source)"
    publisher: rust-lang/cargo (GitHub)
    url: https://github.com/rust-lang/cargo/blob/master/doc/book/src/reference/build-scripts.md
    accessed: 2026-11-17
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-cargo-new-project](https://iinventedlight.com/library/notes/rust-cargo-new-project/)
- [rust-cargo-workspaces](https://iinventedlight.com/library/notes/rust-cargo-workspaces/)
- [rust-cargo-features](https://iinventedlight.com/library/notes/rust-cargo-features/)
- [rust-cargo-test-options](https://iinventedlight.com/library/notes/rust-cargo-test-options/)

## 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_DIR` environment variable]"

- id: C4
  claim: Cargo does not clean OUT_DIR between builds.
  source: S2
  evidence: "Cargo does not clean or reset `OUT_DIR` between 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
