I am trying to do simple telegram bot. I have to response some answer for provided questions.
Problem is that I can’t use borrowed parts of question (string) to pass through it to db save function.
I’ve cut my code as mush as possible:
pub enum Answer {
DbCommand(Box<dyn Fn()>),
}
pub fn process(question: &str) -> Answer {
let parts: Vec<&str> = question
.split(" ")
.collect();
let channel = parts.get(1).unwrap();
Answer::DbCommand(Box::new(|| {
save_to_db(channel)
}))
}
pub fn save_to_db(chan: &str) {
// Saving to db
}
Output is:
error[E0621]: explicit lifetime required in the type of `question`
--> src/lib.rs:12:23
|
5 | pub fn process(question: &str) -> Answer {
| ---- help: add explicit lifetime `'static` to the type of `question`: `&'static str`
...
12 | Answer::DbCommand(Box::new(|| {
| _______________________^
13 | | save_to_db(channel)
14 | | }))
| |______^ lifetime `'static` required
If I add some function lifetime, then I get error E0495. There’s not much information on it
2
Answers
My final code looks like this:
split
does not allocate anything, it only iterates over the initial string, keeping a reference to it. You need to own the string and move it into the closure:By the way, you do not need to collect anything in this case.
If you really don’t want to allocate a string, you can make your struct generic over a lifetime, but I think that this would add an unneeded complexity.:
That’s because a trait object has an implicit
'static
lifetime by default.