extern crate env_logger;
extern crate ws;
use ws::{connect};
use std::thread;
use std::sync::mpsc;
fn main() {
// Setup logging
env_logger::init();
let (tx, rx) = mpsc::channel::<i32>();
thread::spawn(move || {
connect("wss://socket.coinex.com", |out| {
let query = "{\"method\": \"depth.subscribe\", \"id\": 15, \"params\": [\"BTCUSDT\", 50, \"0.01\"]}";
out.send(query).unwrap();
move |msg| {
println!("Got message: {}", msg);
tx.send(1).unwrap();
Ok(())
}
}).unwrap();
});
loop {
let received = rx.recv().unwrap();
println!("Got: \n{:#?}", received);
}
}
[dependencies]
env_logger = "*"
ws = { version = "*", features = ["ssl"]}
编译错误:
Compiling market_maker v0.1.0 (/home/ubuntu/autotrade/rust/market_maker)
error[E0507]: cannot move out of `tx`, a captured variable in an `FnMut` closure
--> src/main.rs:19:7
|
12 | let (tx, rx) = mpsc::channel::<i32>();
| -- captured outer variable
...
19 | move |msg| {
| ^^^^^^^^^^ move out of `tx` occurs here
20 | println!("Got message: {}", msg);
21 | tx.send(1).unwrap();
| --
| |
| move occurs because `tx` has type `std::sync::mpsc::Sender<i32>`, which does not implement the `Copy` trait
| move occurs due to use in closure
error: aborting due to previous error
For more information about this error, try `rustc --explain E0507`.
std::sync::mpsc::Sender< i32 >怎么实现Copy trait?
新手,好多概念还不熟,多谢! 感叹rust真的是随便写点东西都要过一遍所有语言特性。
1
共 3 条评论, 1 页
评论区
写评论actix actor 比线程好用。
原来clone放错地方了,在move之前clone就好了。 谢谢!
对以下内容的回复:
无脑clone就对了。