intendednull / yewdux

Ergonomic state management for Yew applications
https://intendednull.github.io/yewdux/
Apache License 2.0
322 stars 31 forks source link

Cannot move out of ... #47

Closed TiemenSch closed 1 year ago

TiemenSch commented 1 year ago

I can't seem to wrap my head around why the following is happening:

#[derive(Clone, Debug, Default, PartialEq, Store)]
pub struct Foo {
    foo: String,
}
impl Foo {
    pub fn update(&mut self, new: String) {
        self.foo = new;
    }
}

#[function_component]
pub fn Tabs() -> Html {
    let (foo, foo_dp) = use_store::<Foo>();

    let id = uuid::Uuid::new_v4().to_string();
    let update = {
        let id = id.clone();
        foo_dp.reduce_mut_callback_with(|foo, e: MouseEvent| {
            foo.update(id);
        })
    };

    //    html!  and friends ...

Resulting in an error on the update call stating:

cannot move out of `id`, a captured variable in an `Fn` closure
move occurs because `id` has type `std::string::String`, which does not implement the `Copy` trait

With the latest yewdux and yew as dependencies.

Any ideas?

TiemenSch commented 1 year ago

Perhaps to clarify, this does compile and I can't seem to find the difference https://github.com/intendednull/yewdux/blob/ae66ae4b5ccf47fcbfaa1185327ab9ff432a3ac7/examples/async_proxy/src/main.rs#L45

TiemenSch commented 1 year ago

It was the move |...| before the closure arguments, although I could've sworn I tried that this morning, too.

Sorry to have bothered you with this.