- Efficient Solution using
std::process::Command
Most efficient way of copying the build result into a string variable will be using std::process::Command to run the cargo build command and then capture and parse the output.
use std::process::Command;
use std::str;
fn main() {
let output = Command::new("cargo")
.arg("build")
.output()
.expect("Failed to execute 'cargo build'");
let build_results = str::from_utf8(&output.stdout).unwrap();
println!("Build Results: \n{}", build_results);
}
- Solution using
std::process::Command and collecting the output into a Vec<u8>
This is a similar solution to the first one but using a Vec<u8> to store the output.
use std::process::Command;
use std::str;
fn main() {
let output = Command::new("cargo")
.arg("build")
.output()
.expect("Failed to execute 'cargo build'");
let build_results = String::from_utf8(output.stdout).unwrap();
println!("Build Results: \n{}", build_results);
}
- Solution using
std::process::Command and std::io::Read
This solution reads the output in chunks using std::io::Read.
use std::process::{Command, Stdio};
use std::io::Read;
fn main() {
let mut child = Command::new("cargo")
.arg("build")
.stdout(Stdio::piped())
.spawn()
.expect("Failed to execute 'cargo build'");
let mut build_results = String::new();
child.stdout.as_mut().unwrap().read_to_string(&mut build_results).unwrap();
println!("Build Results: \n{}", build_results);
}
- Solution using
std::thread and std::sync
This solution reads the output in another thread and uses a std::sync::mpsc channel to send the output back.
use std::process::{Command, Stdio};
use std::io::Read;
use std::thread;
use std::sync::mpsc;
fn main() {
let mut child = Command::new("cargo")
.arg("build")
.stdout(Stdio::piped())
.spawn()
.expect("Failed to execute 'cargo build'");
let (tx, rx) = mpsc::channel();
let stdout = child.stdout.take().unwrap();
let read_thread = thread::spawn(move || {
let mut buffer = String::new();
let mut reader = std::io::BufReader::new(stdout);
reader.read_to_string(&mut buffer).unwrap();
buffer
});
let build_results = rx.recv().unwrap();
println!("Build Results: \n{}", build_results);
}
- Solution using
std::process::Command with the Stdio::inherit() method
This solution uses the Stdio::inherit() method to inherit the parent process stdout.
use std::process::{Command, Stdio};
use std::io::Read;
fn main() {
let output = Command::new("cargo")
.arg("build")
.stdout(Stdio::inherit())
.output()
.expect("Failed to execute 'cargo build'");
// Build results are visible in the console, but not stored in the string.
}
- Completely different approach: Wrapping the build command in a shell script
One of the completely different approaches is wrapping the build command in a shell script that writes the output to a file, and then read that file in your Rust code.
# build.sh
#!/usr/bin/env bash
OUTPUT_FILE="build_results.txt"
cargo build 2>&1 | tee "$OUTPUT_FILE"
Then read the file in your Rust code:
use std::fs;
fn main() {
let output = std::process::Command::new("./build.sh")
.status()
.expect("Failed to execute build script");
let build_results = fs::read_to_string("build_results.txt").unwrap();
println!("Build Results: \n{}", build_results);
}
- Completely different approach: Use
cargo_metadata crate
Another completely different approach is using an external crate like cargo_metadata to obtain some information about the build process, and then, you can process this information as you want.
Here's an example of using cargo_metadata:
[dependencies]
cargo_metadata = "0.12.3"
use cargo_metadata::{MetadataCommand, Message};
fn main() {
let metadata = MetadataCommand::new().exec().unwrap();
for package in metadata.packages {
println!("Processing package: {}", package.name);
let build = std::process::Command::new("cargo")
.args(&["build", "-p", &package.name])
.output()
.expect("Failed to run 'cargo build'");
let build_result = String::from_utf8_lossy(&build.stdout);
// Process the build result as you wish.
}
}