#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let bot = Bot::new(TG_TOKEN);
let mut users = load_users()?;
teloxide::repl(bot, |bot: Bot, msg: Message| async move{
let text = msg.text().unwrap();
match text {
"/start" => {
bot.send_message(msg.chat.id, TEXT1).await.unwrap();
let user = User::new(msg.chat.username().unwrap().to_string(), msg.chat.id);
users.insert(user);
}
"/status" => { bot.send_message(msg.chat.id, TEXT2).await.unwrap(); }
&_ => {}
};
Ok(())
}).await;
Ok(())
}
I need to save and update users collection, it’s HashSet when user is calling /start
but i get this error
Expected a closure that implements the Fn
trait, but this closure only implements FnOnce
Here is full error from cargo check
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
--> srcmain.rs:39:22
|
39 | teloxide::repl(bot, |bot: Bot, msg: Message| async move{
| -------------- -^^^^^^^^^^^^^^^^^^^^^^^
| | |
| _____|___________________this closure implements `FnOnce`, not `Fn`
| | |
| | required by a bound introduced by this call
40 | | let text = msg.text().unwrap();
41 | |
42 | | match text {
... |
46 | | users.insert(user);
| | ----- closure is `FnOnce` because it moves the variable `users` out of its environment
... |
53 | | Ok(())
54 | | }).await;
| |_____- the requirement to implement `Fn` derives from here
|
= note: required for `{closure@srcmain.rs:39:22: 39:46}` to implement `Injectable<DependencyMap, Result<(), RequestError>, (Bot, teloxide::prelude::Message)>`
note: required by a bound in `teloxide::repl`
--> C:Usersgogle.cargoregistrysrcindex.crates.io-6f17d22bba15001fteloxide-0.12.2srcreplsrepl.rs:56:8
|
52 | pub async fn repl<R, H, Args>(bot: R, handler: H)
| ---- required by a bound in this function
...
56 | H: Injectable<DependencyMap, ResponseResult<()>, Args> + Send + Sync + 'static,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `repl`
For more information about this error, try `rustc --explain E0525`.
error: could not compile `jaser_bot` (bin "jaser_bot") due to 1 previous error
idc how to fix this
I tried to create a wrapper around collection of users with ‘a lifetime, but this doesn’t help