Joining a thread in a method that takes `&mut self` (like drop) results in “cannot move out of borrowed content”

后端 未结 4 2043
梦如初夏
梦如初夏 2020-12-19 06:21

I want to create a thread inside of the new method and stop it after the struct is destroyed:

use std::thread;

struct Foo {
    handle: thread:         


        
4条回答
  •  孤城傲影
    2020-12-19 06:54

    The problem in join signature:

    fn join(self) -> Result
    

    so to fix your code, you need something like:

    pub fn stop(self) {
        self.handle.join();
    }
    

提交回复
热议问题