2

I'm using the ws crate to make a single-threaded WebSocket server. I've reproduced the problem here:

extern crate rustcord;
extern crate ws;

use rustcord::{Rustcord, User, EventHandlers};
use ws::listen;

struct Handlers;

impl EventHandlers for Handlers {
    fn ready(_user: User) { }
}

fn main() {
    let discord = Rustcord::init::<Handlers>("<client_id>", true, None)
        .expect("Failed");

    listen("127.0.0.1:8080", |out| {
        move |msg| {
            discord.clear_presence();
            out.send(msg)
        }
    });
}

Here's the output from cargo check:

error[E0507]: cannot move out of `discord`, a captured variable in an `FnMut` closure
  --> src/main.rs:18:9
   |
14 |     let discord = Rustcord::init::<Handlers>("<client_id>", true, None)
   |         ------- captured outer variable
...
18 |         move |msg| {
   |         ^^^^^^^^^^ move out of `discord` occurs here
19 |             discord.clear_presence();
   |             -------
   |             |
   |             move occurs because `discord` has type `rustcord::Rustcord`, which does not implement the `Copy` trait
   |             move occurs due to use in closure

Here is my Cargo.toml file:

[package]
name = "fooproject"
version = "0.1.0"
authors = ["entity100011"]
edition = "2018"

[dependencies]
ws = "=0.8.1"
rustcord = "=0.2.2"

I'm new to Rust and don't grasp the whole concept of "closures" and other Rust things. There may be other questions with the solution for this, but I can't understand the code there.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • It looks like your question might be answered by the answers of [Cannot move out of captured variables in an `FnMut` closure](https://stackoverflow.com/q/53038935/155423); [How can I move a captured variable into a closure within a closure?](https://stackoverflow.com/q/28521637/155423); [Cannot move out of captured outer variable in an Fn closure](https://stackoverflow.com/q/50000453/155423); etc.. If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jul 23 '19 at 17:48
  • @Shepmaster Okay, you know what, I'll just post what I'm actually trying to do – Krey Lazory Jul 23 '19 at 18:11
  • @Shepmaster I don't understand what closures are, I think. I tried learning about them in the docs, but they're really complicated – Krey Lazory Jul 23 '19 at 18:14
  • @KreyLazory I'm stuck on exactly the same problem with ws-rs, where I'm trying to get the server's ws::Sender value out so that I can loop over some other channel and broadcast to clients. I think the way I solved it last time was to use a completely different library. – Petrus Theron Jul 24 '19 at 16:21
  • Solved it by using messaging channels and separate threads. – Krey Lazory Aug 23 '19 at 10:09
  • Does this answer your question? [Cannot move out of borrowed content when trying to transfer ownership](https://stackoverflow.com/questions/28258548/cannot-move-out-of-borrowed-content-when-trying-to-transfer-ownership) – Aditya Giri Mar 21 '22 at 15:16

0 Answers0