I am working on my first Rust project, a CLI application to find large files on a local filesystem. Rust has excellent documentation regarding match statements but I do not see how I can assign a value returned by the function passed to the match statement in a new variable:
pub fn run(config: Config) -> Result<(), Box<dyn Error>> { let mut results_map: Option<Results>; match search(&config.root_path) { Err(e) => eprintln!("Error when calling run(): {:?}", e), Ok(results) => results_map = results), // how to properly assign value returned by function here? } Ok(()) } pub struct Results { result_map: HashMap<String, u64>, } pub fn search(path: &str) -> Result<Results, io::Error> { let root_path = Path::new(path); let mut results = Results { result_map: HashMap::<String, u64>::new()}; match visit_dirs(root_path, &mut results) { Err(e) => eprintln!("Error calling visit_dirs() from search(): {:?}", e), _ => (), } Ok(results) }
_(dont care). Since you care about the result you should match with an expressionOk(result)instead. As here