Skip to content

Instantly share code, notes, and snippets.

@daxhuiberts
Created December 8, 2014 14:56
Show Gist options
  • Save daxhuiberts/9da05585979667f7743a to your computer and use it in GitHub Desktop.
Save daxhuiberts/9da05585979667f7743a to your computer and use it in GitHub Desktop.
Bettering my Rust
// old one
fn process_stat(str: &str) -> Option<Vec<u32>> {
match STAT_REGEX.captures(str) {
Some(captures) => {
let iter = captures.at(1).split(' ');
let vec = iter.map(|x: &str| {
from_str(x).unwrap()
}).collect();
Some(vec)
}
None => None
}
}
// new one (also safer because there is no unwrap anymore)
fn process_stat(str: &str) -> Option<Vec<u32>> {
STAT_REGEX.captures(str).and_then( |captures| {
let iter = captures.at(1).split(' ');
iter.map(from_str).collect()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment