I am trying to build a method on self that builds a set of reverse lookup structures based on the information in a passed in structures. I used methods on the parameter to get iterators to get everything that needs to be looked up. (I did have this as a free standing methods. I thought it would be better as a structure method, and hoped that would fix this problem. But it didn't. The compiler is complaining about moves into the closures:
pub fn mk_indices(&mut self, rolo: &RolodexBase) {
self.short_label_list = Vec::new();
self.long_label_list = Vec::new();
self.label_to_cat = HashMap::new();
rolo.short_cat_iter().into_iter().map(|cstr| {
rolo.cat_label_iter(cstr.clone()).into_iter().map(move |label| {
self.short_label_list.push(label.clone());
self.label_to_cat.insert(label.clone(), cstr.clone());
})
});
rolo.long_cat_iter().into_iter().map(|cstr| {
rolo.cat_label_iter(cstr.clone()).into_iter().map(move |label| {
self.long_label_list.push(label.clone());
self.label_to_cat.insert(label.clone(), cstr.clone());
})
});
}
And the compiler errors are:
error[E0507]: cannot move out of `self`, a captured variable in an `FnMut` closure
--> src\rolo_window\person_gui.rs:81:63
|
69 | pub fn mk_indices(&mut self, rolo: &RolodexBase) {
| ---------
| |
| captured outer variable
| move occurs because `self` has type `&mut PeopleGuiState`, which does not implement the `Copy` trait
...
80 | rolo.long_cat_iter().into_iter().map(|cstr| {
| ------ captured by this `FnMut` closure
81 | rolo.cat_label_iter(cstr.clone()).into_iter().map(move |label| {
| ^^^^^^^^^^^^ `self` is moved here
82 | self.long_label_list.push(label.clone());
| -------------------- variable moved due to use in closure
|
help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but `FnOnce` closures may consume them only once
--> C:\Users\jmh\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\iter\traits\iterator.rs:776:12
|
776 | F: FnMut(Self::Item) -> B,
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0382]: use of moved value: `self`
--> src\rolo_window\person_gui.rs:80:46
|
69 | pub fn mk_indices(&mut self, rolo: &RolodexBase) {
| --------- move occurs because `self` has type `&mut PeopleGuiState`, which does not implement the `Copy` trait
...
74 | rolo.short_cat_iter().into_iter().map(|cstr| {
| ------ value moved into closure here
75 | rolo.cat_label_iter(cstr.clone()).into_iter().map(move |label| {
76 | self.short_label_list.push(label.clone());
| --------------------- variable moved due to use in closure
...
80 | rolo.long_cat_iter().into_iter().map(|cstr| {
| ^^^^^^ value used here after move
81 | rolo.cat_label_iter(cstr.clone()).into_iter().map(move |label| {
82 | self.long_label_list.push(label.clone());
| -------------------- use occurs due to use in closure
The point is to mutate self. Copying it to use in the iterators won't do.
I tried using local variables instead of the self fields, and it gave me similar errors with proposed resolutions that would not leave the constructed information after the iterations.
I presume I ma missing something obvious. But staring at it for a few hours didn't reveal anything. Sorry.
Thanks,
Joel