Skip to content

Instantly share code, notes, and snippets.

@pythonesque
Forked from paulp/slurp.rs
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythonesque/df8c0c9b14cf84f2c1ce to your computer and use it in GitHub Desktop.
Save pythonesque/df8c0c9b14cf84f2c1ce to your computer and use it in GitHub Desktop.
use std::ffi::OsStr;
use std::process::Command;
use std::borrow::Cow;
use std::convert::Into;
pub fn exec_slurp<'a>(cmd: &'a str, args: &[&OsStr]) -> Cow<'a, str> {
println!("exec_slurp({}, {:?})", cmd, args);
Command::new(cmd)
.args(args)
.output()
.ok()
.and_then(|x| String::from_utf8(x.stdout).ok() )
.map(Into::into)
.unwrap_or(Cow::Borrowed(""))
}
pub fn exec_slurp_string(cmd: &str, args: &[&OsStr]) -> String {
println!("exec_slurp({}, {:?})", cmd, args);
Command::new(cmd)
.args(args)
.output()
.ok()
.and_then(|x| String::from_utf8(x.stdout).ok() )
.unwrap_or(String::new())
}
fn main() {
}
use std::path::Path;
use std::ffi::OsStr;
fn foo<'a>(strings: &[&'a AsRef<Path>]) -> Vec<&'a Path> {
strings.into_iter().map(AsRef::as_ref).collect()
}
fn main() {
foo(&[&"biz".to_string(), &Path::new("foo/bar/baz"), &OsStr::new("zab"), &"baz"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment