相关疑难解决方法(0)

如何为闭包参数声明生命周期?

我想在Rust中为一个闭包声明一个生命周期,但我找不到添加生命周期声明的方法.

use std::str::SplitWhitespace;

pub struct ParserError {
    pub message: String,
}

fn missing_token(line_no: usize) -> ParserError {
    ParserError {
        message: format!("Missing token on line {}", line_no),
    }
}

fn process_string(line: &str, line_number: usize) -> Result<(), ParserError> {
    let mut tokens = line.split_whitespace();

    match try!(tokens.next().ok_or(missing_token(line_number))) {
        "hi" => println!("hi"),
        _ => println!("Something else"),
    }

    // The following code gives "cannot infer appropriate lifetime.....
    // let nt = |t: &mut SplitWhitespace| t.next().ok_or(missing_token(line_number));
    // match try!(nt(&mut tokens)) {
    //     "there" => println!("there"),
    // …
Run Code Online (Sandbox Code Playgroud)

closures lifetime rust

17
推荐指数
3
解决办法
4236
查看次数

标签 统计

closures ×1

lifetime ×1

rust ×1