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.