How to declare a lifetime for a closure argument?

后端 未结 3 747
無奈伤痛
無奈伤痛 2020-11-30 11:10

I would like to declare a lifetime for a closure in Rust, but I can\'t find a way to add a lifetime declaration.

use std::str::SplitWhitespace;

pub struct P         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 11:50

    I don't know how to answer your question, but there are two ways to solve the problem:

    The easiest one is to let the closure reference the iterator directly.

    {
        let mut nt = || tokens.next().ok_or(missing_token(line_number));
        // call the closure as many times as you need to
    }
        // At this point `tokens` will be usable again.
    

    If you don't actually need do anything else with tokens afterwards, just do:

    let mut nt = || tokens.next().ok_or(missing_token(line_number)); 
    

    The other solution is to write a function that emulates what the closure is doing and call that instead.

提交回复
热议问题