Borrow self in spawn

error[E0596]: cannot borrow `self.rx_nonce_data` as mutable, as `self` is not declared as mutable
   --> src/miner.rs:404:36
    |
330 |     pub fn run(self) {
    |                ---- help: consider changing this to be mutable: `mut self`
...
404 |             while let Some(item) = self.rx_nonce_data.next().await {
    |                                    ^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error[E0382]: use of moved value: `self`
   --> src/miner.rs:403:31
    |
399 |           let account_id_to_target_deadline = self.account_id_to_target_deadline;
    |                                               ---------------------------------- value moved here
...
403 |           task::spawn(async move{ 
    |  _______________________________^
404 | |             while let Some(item) = self.rx_nonce_data.next().await {
    | |                                    ---- use occurs due to use in generator
405 | |
406 | |         } });
    | |___________^ value used here after partial move
    |
    = note: move occurs because `self.account_id_to_target_deadline` has type `std::collections::HashMap<u64, u64>`, which does not implement the `Copy` trait

why i am wrong? what's move do?

Sounds like you need to add mut here:

pub fn run(mut self) {

And it sounds like you need to clone account_id_to_target_deadline.

why does my pass by edit this

  let mut rx_nonce_data = self.rx_nonce_data;
        task::spawn(async move {
            while let Some(nonce_data) = rx_nonce_data.next().await {
            }
        });

but if i change self.rx_nonce_data the code will be wrong

        task::spawn(async move {
            while let Some(nonce_data) = self.rx_nonce_data.next().await {

            }
        });

error[E0596]: cannot borrow `self.rx_nonce_data` as mutable, as `self` is not declared as mutable
   --> src/miner.rs:405:42
    |
330 |     pub fn run(self) {
    |                ---- help: consider changing this to be mutable: `mut self`
...
405 |             while let Some(nonce_data) = self.rx_nonce_data.next().await {
    |                                          ^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error[E0382]: use of moved value: `self`
   --> src/miner.rs:404:32
    |
399 |           let account_id_to_target_deadline = self.account_id_to_target_deadline;
    |                                               ---------------------------------- value moved here
...
404 |           task::spawn(async move {
    |  ________________________________^
405 | |             while let Some(nonce_data) = self.rx_nonce_data.next().await {
    | |                                          ---- use occurs due to use in generator
406 | |
407 | |             }
408 | |
409 | |         });
    | |_________^ value used here after partial move
    |
    = note: move occurs because `self.account_id_to_target_deadline` has type `std::collections::HashMap<u64, u64>`, which does not implement the `Copy` trait

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.