From ea5ae5968a688789bf46e4cc1391a54bc4bc502c Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 12 Dec 2024 02:39:54 +0000 Subject: [PATCH] use filter().map() --- src/main.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index cd46cd7..7793f0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,23 +129,22 @@ fn get_layouts() -> Vec { let mut layout_names: Vec = Vec::new(); // Get the names of monitors that are available (that match a current output) - let mut available_monitor_names: Vec = Vec::new(); - // TODO do this with iterators - rules.monitors.iter().for_each(|(monitor_name, monitor)| { - if available_outputs.iter().any(|output| { - monitor.make == output.make - && monitor.model == output.model - && output.serial == monitor.serial - }) { - available_monitor_names.push(String::from(monitor_name)); - } - }); + let available_monitor_names:Vec<&String> = rules.monitors.iter() + .filter(|(_monitor_name,monitor)| + available_outputs.iter().any(|output| + monitor.make == output.make + && monitor.model == output.model + && output.serial == monitor.serial + ) + ) + .map(|(monitor_name,_monitor)| monitor_name) + .collect(); // Add each layout defined in the config file for which all outputs are available rules.layouts.iter().for_each(|(layout_name, layout)| { if layout .iter() - .all(|(output_name, _output)| available_monitor_names.contains(output_name)) + .all(|(output_name, _output)| available_monitor_names.contains(&output_name)) { layout_names.push(String::from(layout_name)) }