鉴于此结构:
struct Token<'a> {
r: &'a Regex,
f: &'a Fn(String) -> Symbol
}
从fn:
返回时,我无法找出正确的语法来指定闭包生命周期fn get_tokens<'a>() -> Vec<Token<'a>> {
static ref NUMBER: Regex = Regex::new(r"$(\d+|\d*\.\d+)").unwrap(); // TODO avoid "000"?
let num_token = Token{ r: &*NUMBER, f: &|s| Symbol::Number(s) };
let tokens = vec![num_token];
tokens
}
如果没有生命周期参数,我会得到借来的值不够长的错误:
error: borrowed value does not live long enough
--> src\syntax\lex.rs:49:45
|
49 | let num_token = Token{ r: &*NUMBER, f: &|s| Symbol::Number(s) };
| ^^^^^^^^^^^^^^^^^^^^^ temporary value created here
...
53 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the block at 36:38...
--> src\syntax\lex.rs:36:39
|
36 | fn get_tokens<'a>() -> Vec<Token<'a>> {
| ^
我找不到任何显示为这样的闭包指定生命周期参数的示例。