I have been slowly teaching myself some Rust, and in my latest endeavor, I am trying my hand at implementing some of the Maelstrom 'workloads' in Rust.
As part of the Broadcast 'workload', I am trying to keep track of a list of messages other 'nodes' know about, and return only a set of messages that they don't, plus a few random extras (just in case some messages get 'dropped' or something..?)...
But I'm running into some weirdness I can't understand. Here's a snippet of what I'm trying to do (full source here):
// start by getting all the values we know the src node doesn't know
let src_known = self.neighbors_known_msgs.get(&msg.src).unwrap(); // this ends up being a HashSet<usize> (already checked it exists earlier in method)
let mut src_unknown: HashSet<_> = self.known_msgs
.difference(src_known)
.copied()
.collect();
// Now extend that list with an extra set of values
let list:Vec<_> = self.known_msgs.iter().copied().collect();
let mut window_size = MIN_WIDOWING_SIZE.max(list.len()/5);
window_size = window_size.min(list.len());
// for a first pass, just select the extras randomly,
// before trying to implement proper windowing
let mut rng = rand::thread_rng();
let extras = (0..=window_size).filter_map(|_| {
let idx: usize = rng.gen();
list.get(idx)
}).cloned();
// at this point, we have a list of 20% of of the
// total messages we know about in the 'extras' range/iterator
src_unknown.extend(extras);
// `src_unknown` is unchanged at this point...???
// and `extras` is not exhausted...???
I setup a test where the self.known_msgs
and self.neighbors_known_msgs.get(&msg.src)
have exactly the same set of 100 values, so a .difference(..)
results in set of no values.
So I would expect the extras
with 20 values (which shows up in debug), would be the only values in src_unknown
after the src_unknown.extend(extras);
statement executes...
But the actual result is src_unknown
is unchanged, and the extras
is still a Cloned<FilterMap<RangeInclusive<usize>
that is showing iter:{start:0, end:20, exhausted:false}
in the debugger...??
Im at a loss to understand what is going wrong here... Help! (And thanks in advance!)