Skip to content

Instantly share code, notes, and snippets.

@barafael
Created March 23, 2017 18:19
Show Gist options
  • Save barafael/646e3b88c1b2bdccb7f82e921af1ae52 to your computer and use it in GitHub Desktop.
Save barafael/646e3b88c1b2bdccb7f82e921af1ae52 to your computer and use it in GitHub Desktop.
Confused about error handling
use std::process::Command;
fn main() {
println!("{}", git_name().unwrap_or("".to_string()));
}
pub fn git_name() -> Option<String> {
let output = Command::new("git")
.arg("config")
.arg("user.name")
.output()
.expect("Couldn't run git!");
if output.status.success() {
let s = String::from_utf8_lossy(&output.stdout);
print!("git config user.name: {}", s);
Some(s.to_string())
} else {
let s = String::from_utf8_lossy(&output.stderr);
print!("git config user.name failed! {}", s);
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment