let (channel_tx, channel_rx) = create_channel();
let mut listeners = VecDeque::new();
for settings in config.interfaces.iter() {
listeners.push_back(MyListener::new(settings, channels_tx.clone());
}
mem::drop(channels_tx);
/* and so on...*/
Now I think it is unnecessary wasteful to create a separate clone of channel_tx for each MyListener instance and drop the original channel_tx object afterwards. I'd rather create a clone of channel_tx for each MyListener instance except for the last one, and make the last one consume the original channel_tx object. What is an elegant way to do this?
I think about something like this:
for settings in config.interfaces.iter() {
let my_channel = if IS_LAST_ONE { channels_tx } else { channels_tx.clone() };
listeners.push_back(MyListener::new(settings, my_channel);
}
But how to "detect" the last iteration of the loop?
let (channel_tx, channel_rx) = create_channel();
let mut listeners = VecDeque::new();
let mut iter = config.interfaces.iter().fuse();
let mut prev_settings = iter.next();
for next_settings in iter {
let settings = prev_settings.take().unwrap(); // Guaranteed to be Some(...)
listeners.push_back(MyListener::new(settings, channels_tx.clone());
prev_settings = Some(next_settings);
}
if let Some(settings) = prev_settings { // Maybe None, when there wasn't any interface
listeners.push_back(MyListener::new(settings, channels_tx);
}
Instead looking into the future (when you cannot), defer the action/decision and first take the step into that future and then look back in the past or act in the past.