如何将 &str 生命周期传递给 Box<dyn Fn()>

How to pass through &str lifetime to Box<dyn Fn()>

我正在尝试做简单的电报机器人。我必须对提供的问题做出一些回答。

问题是我无法使用问题(字符串)的借用部分将其传递给数据库保存功能。

我已经尽可能精简了我的代码:

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
}

Playground

输出为:

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
}

这是因为 trait 对象默认有一个隐式的 '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()),
    }
}