Rust - Build Scripts Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In Rust, there is an option for building scripts where Cargo compiles a built script into an executable file, and after running the script, many tasks can be performed. There are scenarios where we need different options for generating a code before it's built. The cargo compiler in this case compiles the script and integrates it with build scripts. To execute this step, we need to declare a build.rs file in the root directory. Example: In this example, we declare a file build.rs In the main.rs file, the code is: Rust include!(concat!(env!("OUT_DIR"), "/gfg.rs")); fn main() { println!("Printing: {}", message()); } In the build.rs, the code is: Rust use std::env; use std::fs; use std::path::Path; fn main() { let out_dir = env::var_os("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("gfg.rs"); fs::write( &dest_path, "pub fn message() -> &'static str { \"Geeks for Geeks!\" } " ).unwrap(); println!("cargo:rerun-if-changed=build.rs"); } Output: For getting the output, we put cargo run Explanation: In the main.rs file, we have declared an expression include! concat! and env! which means that the library build.rs is defined in the rust module and once we use the concat! macro and the env! macro the gfg.rs file is generated for the rust crate to compile. In the build.rs file, the Out_dir envt variable is declared for knowing the location of the output files. Also, the rerun-if-changed instruction tells cargo that the scripts would be re-run in case the build script is changed and this line ensures that cargo automatically runs if any build script changes with a change in the package. Comment More infoAdvertise with us Next Article Rust - Tests S sayanc170 Follow Improve Article Tags : Technical Scripter Rust Technical Scripter 2022 Rust-basics Similar Reads Rust - Strings String data type is a very important part of any programming language. Rust handles strings a bit differently from other languages. Â The String data type in Rust is of two types: String Literal (&str)String Object (String)String LiteralString Literal or &str are called 'string slices', whic 2 min read Rust - Generic Traits Rust is an extremely fast programming language that supports speed, concurrency, as well as multithreading. In Rust, we have a concept of Generic Traits where Generics are basically an abstraction of various properties, and Traits are basically used for combining generic types for constraining types 2 min read Rust - Tests In Rust, there are specific procedures that are needed to be adhered to before writing the tests. The steps for writing the tests are as follows: Step 1: Setting up data/states required by our code. Step 2: Run the code for the tests we need to test. Step 3: Asserting the expected results. In Rust, 2 min read Variables in Rust Variables are memory addresses used to store information to be referenced and manipulated in programs. They also provide a way of defining data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that 4 min read How To Run Bash Script In Linux? Bash scripts, also called shell scripts, are programs that help automate tasks by storing a series of commands that often go together, like updates or upgrades. These scripts make it easier to perform tasks automatically instead of typing each command manually. After creating a Bash script, you can 6 min read Rust - Switch Case Switch statement is basically nested if statement, though it does not support expression, it matches an int, string, or boolean data type variable with a given set of cases that are provided by the programmer. It is mainly used in a menu-driven program where the user selects which function they want 2 min read Like