Using non-copyable type in for loop


#1

Hey guys here’s the situation, I’m using https://github.com/nickel-org/rust-mustache to add some templating to an Iron project.

I have a vector of strings that contain error messages to render to the template. I want to loop the errors and insert them in to a map using the build in map builder. Consider the following code:

template_data.insert_map("errors", |builder| {

            for error in &mut errors {

                builder.insert_str("error", error);

            }

            builder

        })

This of course generate said error:

src\main.rs:108:17: 108:24 error: use of moved value: `builder` [E0382]
src\main.rs:108                 builder.insert_str("error", error);
                                ^~~~~~~
src\main.rs:108:17: 108:24 help: run `rustc --explain E0382` to see a detailed explanation
note: `builder` was previously moved here because it has type `mustache::builder::MapBuilder`, which is non-copyable
src\main.rs:112:13: 112:20 error: use of moved value: `builder` [E0382]
src\main.rs:112             builder

Any help would be appreciated, I’ve been searching for hours with no luck.


#2

Does this work?

    template_data.insert_map("errors", |mut builder| {
            for error in &mut errors {
                builder = builder.insert_str("error", error);
            }
            builder
        })

#3

It sure does, thanks x1million!