'Rust: Move issue in while loop
I have the following rust code and for the life of me I cant workout why I am getting a borrow-checker issue with this value and why does it get moved as part of a while loop.
Code:
#[tokio::main]
async fn main() -> Fallible<()> {
let config = Config::new().expect("config can't be loaded");
logger::config(&config.log);
let bn = Binance::with_credential(
&config.exchanges.binance_com.api_key,
&config.exchanges.binance_com.api_secret,
);
let mut db = Database::new(config).await;
match bn.user_stream_start()?.await {
Ok(_) => {
let mut ws = BinanceWebsocket::default();
for sub in vec![Subscription::Trade("btcusdt".to_string())] {
ws.subscribe(sub).await?;
}
while let Some(msg) = ws.try_next().await? {
db.write(msg).await?
}
}
Err(e) => error!("Error obtaining stream: {}", e),
};
Ok(())
}
Error:
error[E0382]: use of moved value: `db`
--> x/src/main.rs:35:17
|
24 | let mut db = Database::new(config).await;
| ------ move occurs because `db` has type `Database`, which does not implement the `Copy` trait
...
35 | db.write(msg).await?
| ^^ value moved here, in previous iteration of loop
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0382`.
Any help much appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|