From 55a6fdb7fdadd09ca93f91bb661a57942fd6ed8b Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 5 Jan 2017 15:51:45 -0500 Subject: Add std::process::Command::envs() Command::envs() adds a vector of key-value pairs to the child process environment all at once. Suggested in #38526. --- src/libstd/process.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/process.rs b/src/libstd/process.rs index e15c37aaf24..676ae286eaa 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -377,6 +377,38 @@ impl Command { self } + /// Add or update multiple environment variable mappings. + /// + /// # Examples + /// + /// Basic usage: + /// ```no_run + /// use std::process::{Command, Stdio}; + /// use std::env; + /// + /// let filtered_env : Vec<(String, String)> = + /// env::vars().filter(|&(ref k, _)| + /// k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH" + /// ).collect(); + /// + /// Command::new("printenv") + /// .stdin(Stdio::null()) + /// .stdout(Stdio::inherit()) + /// .env_clear() + /// .envs(&filtered_env) + /// .spawn() + /// .expect("printenv failed to start"); + /// ``` + #[stable(feature = "process", since = "1.16.0")] + pub fn envs(&mut self, vars: &[(K, V)]) -> &mut Command + where K: AsRef, V: AsRef + { + for &(ref key, ref val) in vars { + self.inner.env(key.as_ref(), val.as_ref()); + } + self + } + /// Removes an environment variable mapping. /// /// # Examples -- cgit 1.4.1-3-g733a5