我正在使用crate clap
v4。当我试图编写一些验证正则表达式参数的东西时,我遇到了一些生存期问题。
- ValueParser文档以方便使用
我的代码如下:
pub fn validator_regex(r: &'static str) -> ValueParser {
ValueParser::from(move |s: &str| -> std::result::Result<&str, Error> {
let reg = regex::Regex::new(r).unwrap();
match reg.is_match(s) {
true => Ok(s),
false => Err(Error::from(format!("not matches {}", r))),
}
})
}
pub fn validator_identifier() -> ValueParser {
validator_regex("^[-_0-9a-zA-Z]+$")
}
和编译错误:
error: lifetime may not live long enough
--> main\./src\cmd\cmd_util.rs:440:21
|
437 | ValueParser::from(move |s: &str| -> std::result::Result<&str, Error> {
| - - let's call the lifetime of this reference `'2`
| |
| let's call the lifetime of this reference `'1`
...
440 | true => Ok(s),
| ^^^^^ returning this value requires that `'1` must outlive `'2`
在这两个问题上,有谁能帮我吗?
- 如何在clap v4中验证正则表达式
- 为什么这段代码中有
lifetime may not live long enough
错误,因为返回的&str
和闭包参数一样长,并且可以正常编译
pub fn validator_regex(r: &'static str) -> impl Fn(&str) -> Result<&str, String> {
move |s: &str| -> Result<&str, String> {
let reg = regex::Regex::new(r).unwrap();
match reg.is_match(s) {
true => Ok(s),
false => Err(format!("not match {}", r)),
}
}
}
1条答案
按热度按时间jpfvwuh41#
让我们看看您正在调用的
From
impl:好的,看一下
TypedValueParser
:所以,签名不是
Fn(&str) -> Result<&str, E>
,而是<T> Fn(&str) -> Result<T, E>
,有区别吗?我们不能用&str
代替T
吗?这是非常不同的。让我们来注解一下生命周期:
'b
从何而来?它不能是HRTB,因为它在impl
头文件中声明(T
)而不是特性绑定(Fn
)。因此,它必须是一个固定的生存期。看到不匹配的地方了吗?我称为'a
和'b
的生存期就是编译器称为'1
和'2
的生存期。如果'b
是固定的,则它不能从动态HRTB'a
导出。解决办法很简单:只是不要返回
&str
,而是返回String
: