Browse Source

This is more in line with what I want

master
Julio Biason 2 years ago
parent
commit
033563f631
  1. 46
      mapoptiontest/src/main.rs

46
mapoptiontest/src/main.rs

@ -1,17 +1,35 @@
fn main() { use std::env;
let all_evens = [2u8, 4, 6, 8]; use std::path::PathBuf;
let result: Result<Vec<_>, _> = all_evens
.iter() fn split(line: &str) -> Option<(String, Vec<String>)> {
.map(|value| if value % 2 == 0 { Some(value) } else { None }) let mut split = line.split(' ');
.map(|opt| opt.ok_or(format!("Not good"))) let root = split.next()?;
.collect(); let arguments = split.map(|x| x.to_string()).collect();
println!("All Evens: {:?}", result); Some((root.to_string(), arguments))
}
let not_evens = [2, 2, 2, 2, 2, 1]; fn as_command(command: &str) -> Option<PathBuf> {
let result: Result<Vec<_>, _> = not_evens env::split_paths(&env::var_os("PATH")?).find_map(|dir| {
let full_path = dir.join(command);
if full_path.exists() {
Some(full_path)
} else {
None
}
})
}
fn main() {
let commands = ["ls -la", "ls", "exa", "not"];
let result = commands
.iter() .iter()
.map(|value| if value % 2 == 0 { Some(value) } else { None }) .map(|line| split(&line))
.map(|opt| opt.ok_or(format!("Not good"))) .map(|opt| {
.collect(); let (command, args) = opt?;
println!("Not Evens: {:?}", result); let full_command = as_command(&command)?;
Some((full_command, args))
})
.map(|opt| opt.ok_or("Not good enough"))
.collect::<Result<Vec<_>, _>>();
println!("Result: {:?}", result)
} }

Loading…
Cancel
Save