expression
Pass arguments without shell interpretation
Pass each argument separately to a known native executable, not to a shell. The child receives a value with spaces as one argument. This example launches only its own compiled executable in a finite child mode and captures output; it does not invoke a shell or external command. On Windows, special argument parsing by a target program can still differ; avoid batch files and untrusted executables. Fixture: Expected stdout (not executed): literal argument preserved Expected stderr: (empty) Expected exit: 0 Common mistake: Joining arguments into a shell command string or trusting arbitrary child executables.
Formal Syntax & Structure
rust syntaxrust
Command::new(absolute_exe).arg(value).output()Working Example
Example Snippetrust
fn main() { use std::{env, process::Command}; if env::args().nth(1).as_deref() == Some("--child") { println!("{}", env::args().nth(2).unwrap()); return; } let result = Command::new(env::current_exe().unwrap()) .args(["--child", "two words; literal"]) .output().unwrap(); assert!(result.status.success()); assert!(result.stderr.is_empty()); assert_eq!(String::from_utf8(result.stdout).unwrap().trim_end(), "two words; literal"); println!("literal argument preserved");}