These are chat archives for rust-lang/rust

16th
May 2016
Pete Hayes
@petehayes102
May 16 2016 08:37
Hey guys. Is there any way to call a closure without moving it? The following produces a “Cannot move out of borrowed content” error.
let mut map: HashMap<&str, Box<FnOnce()>> = HashMap::new();
map.insert("moo", Box::new(|| { println!("cow"); }));

match map.get("moo") {
    Some(c) => c(),
    None => (),
}
Vladimir Matveev
@netvl
May 16 2016 08:43

@petehayes102 you need to use another kind of closure, e.g. FnMut:

let mut map: HashMap<&str, Box<FnMut()>> = HashMap::new();

BTW, even if you use map.remove("moo") to get Box<FnOnce()> by value, you cannot call it because it is Box but FnOnce::call_once requires by-value self. There is a workaround for it, called FnBox

Pete Hayes
@petehayes102
May 16 2016 08:46
@netvl Thanks mate! It always seems so easy after you explain it… :)
Vladimir Matveev
@netvl
May 16 2016 08:46
you’re welcome :)
Daniel Collin
@emoon
May 16 2016 17:45
is there a way to iterator over a vector taking two values at a time and skip two forward?
Sean Perry
@shaleh
May 16 2016 17:54
.take(2).skip(2)
Peter Atashian
@retep998
May 16 2016 17:55
@emoon myvec.chunks(2) ?
Daniel Collin
@emoon
May 16 2016 17:58
ah thanks!
Sean Perry
@shaleh
May 16 2016 18:11
chunks is from itertools crate BTW
Vladimir Motylenko
@vldm
May 16 2016 20:20
@shaleh for Iterator, but in std lib we have https://doc.rust-lang.org/std/primitive.slice.html#method.chunks for slice
Sean Perry
@shaleh
May 16 2016 21:21
@vldm correct. Thanks. I was thinking about Iterators and forgot about the collection itself.