[英]How to pass through &str lifetime to Box<dyn Fn()>
我正在尝试做简单的电报机器人。 对于提供的问题,我必须回答一些答案。
问题是我不能使用借来的问题部分(字符串)将其传递给db save函数。
我尽可能地削减我的代码:
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
}
输出是:
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
如果我添加一些函数生命周期,那么我得到错误E0495。 关于它的信息不多
split
不分配任何东西,它只迭代初始字符串,保持对它的引用。 您需要拥有该字符串并将其移动到闭包中:
pub enum Answer {
DbCommand(Box<dyn Fn()>),
}
pub fn process(question: &str) -> Answer {
let channel = question.split(" ").nth(1).unwrap().to_owned();
Answer::DbCommand(Box::new(move || save_to_db(&channel)))
}
pub fn save_to_db(chan: &str) {
// Saving to db
}
顺便说一句,在这种情况下你不需要收集任何东西。
如果你真的不想分配一个字符串,你可以在一生中使你的结构通用,但我认为这会增加一个不必要的复杂性:
pub enum Answer<'a> {
DbCommand(Box<dyn Fn() + 'a>),
}
pub fn process(question: &str) -> Answer {
let channel = question.split(" ").nth(1).unwrap();
Answer::DbCommand(Box::new(move || save_to_db(channel)))
}
pub fn save_to_db(chan: &str) {
// Saving to db
}
那是因为特征对象默认具有隐式的'static
生命周期”。
我的最终代码如下所示:
pub enum Answer<'a> {
Text(String),
DbCommand(Box<dyn Fn() -> Result<String, Error> + 'a>),
}
pub fn process(question: &str) -> Answer {
let mut parts = question
.split(" ")
.map(str::trim)
.filter(|s| !s.is_empty());
let command = parts.next();
match command {
//...
Some("/subscribe") => {
match parts.next() {
Some(channel) => {
Answer::DbCommand(Box::new(move || {
db::subscribe_to_channel(&channel)
//...
}))
},
None => Answer::Text("Provide channel name".into()),
}
},
_ => Answer::Text("Invalid command.".into()),
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.