Ah, got it! You want to avoid any fixed-size arrays entirely — even [0; 1024] or [0; 512].
That’s totally doable. In Rust, you can dynamically allocate buffers using Vec<u8> and grow them on demand, or even use a Cursor<Vec<u8>> with BufReader for more advanced behavior.
✅ Goal Recap
Use only heap-allocated, dynamically sized buffers — no fixed-size arrays anywhere.
We’ll still use .read() in a loop (for streaming), but instead of:
let mut buf = [0; 512];
we'll use:
let mut buf = Vec::with_capacity(512);
or better yet, read directly into a Vec<u8> that grows as needed.
✅ Full Working Example (No Fixed Arrays)
Here's a version that avoids all fixed-size arrays, while still supporting live output and full capture:
use std::io::{self, Read, Write};
use std::process::{Command, Stdio};
use std::thread;
fn run_and_capture(cmd: &str, args: &[&str]) -> io::Result<(String, String)> {
let mut child = Command::new(cmd)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let mut out_pipe = child.stdout.take().unwrap();
let mut err_pipe = child.stderr.take().unwrap();
// Dynamic buffer thread for stdout
let stdout_handle = thread::spawn(move || -> io::Result<Vec<u8>> {
let mut acc = Vec::new();
let mut buf = Vec::with_capacity(1024); // initial capacity
loop {
// Ensure the buffer has space to read into
if buf.len() == buf.capacity() {
buf.reserve(1024); // grow as needed
}
// SAFETY: We're not reading uninitialized memory here.
// Safety is maintained by ensuring we only extend the length
// based on how many bytes were actually read.
unsafe {
let spare = buf.spare_capacity_mut();
let buf_slice = &mut spare[..]; // slice of raw memory
match out_pipe.read(buf_slice) {
Ok(0) => break,
Ok(n) => {
unsafe { buf.set_len(buf.len() + n) }; // update length
acc.extend_from_slice(&buf[buf.len() - n..]);
io::stdout().write_all(&buf[buf.len() - n..]).ok();
io::stdout().flush().ok();
}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
}
}
}
Ok(acc)
});
// Same for stderr
let stderr_handle = thread::spawn(move || -> io::Result<Vec<u8>> {
let mut acc = Vec::new();
let mut buf = Vec::with_capacity(1024);
loop {
if buf.len() == buf.capacity() {
buf.reserve(1024);
}
unsafe {
let spare = buf.spare_capacity_mut();
let buf_slice = &mut spare[..];
match err_pipe.read(buf_slice) {
Ok(0) => break,
Ok(n) => {
unsafe { buf.set_len(buf.len() + n) };
acc.extend_from_slice(&buf[buf.len() - n..]);
io::stderr().write_all(&buf[buf.len() - n..]).ok();
io::stderr().flush().ok();
}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
}
}
}
Ok(acc)
});
let status = child.wait()?;
println!("Child exited with: {}", status);
let stdout_bytes = stdout_handle.join().unwrap()?;
let stderr_bytes = stderr_handle.join().unwrap()?;
let stdout_str = String::from_utf8_lossy(&stdout_bytes).into_owned();
let stderr_str = String::from_utf8_lossy(&stderr_bytes).into_owned();
Ok((stdout_str, stderr_str))
}
fn main() -> io::Result<()> {
let (out, err) = run_and_capture("ping", &["-c", "3", "rust-lang.org"])?;
println!("\n=== STDOUT ===\n{}", out);
println!("=== STDERR ===\n{}", err);
Ok(())
}