expression

Choose a child working directory explicitly

The working directory is process state; it influences relative path resolution. Configure the child rather than changing the parent process directory. Use an absolute executable path because relative executable path resolution with current_dir is platform-dependent. The runner creates a private scratch directory; the fixture creates a nested work directory. Expected stdout (not executed): scratch only Common mistake: Assuming a working directory prevents a child from accessing other directories. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
Command::current_dir(path)

Working Example

Example Snippetrust
use std::{env, fs, io, process::Command};fn main() -> io::Result<()> {    if env::args().nth(1).as_deref() == Some("worker") {        println!("{}", fs::read_to_string("message.txt")?); return Ok(());    }    fs::create_dir("work")?; fs::write("work/message.txt", "scratch only")?;    let out = Command::new(env::current_exe()?).arg("worker").current_dir("work").output()?;    assert!(out.status.success()); print!("{}", String::from_utf8_lossy(&out.stdout));    Ok(())}
Rust Reference