如何解决“指示匿名生命周期<'_>”错误?

问题描述

warning: hidden lifetime parameters in types are deprecated
  --> asd/src/app/qwe.rs:88:45
   |
88 |     fn add_Meta_from_args(&mut self,args: &ArgMatches) -> AppRun {
   |                                             ^^^^^^^^^^- help: indicate the anonymous lifetime: `<'_>`

我应该在哪里指定这个匿名生命周期?我也不太明白它的必要性。如果参数是借用的,为什么还需要一个生命周期?

解决方法

clap 中的 ArgMatches<'a> 结构体在整个生命周期内都是通用的。您没有在函数中写出 args 的完整类型,因为您省略了 ArgMatches 结构的生命周期参数,这就是为什么编译器抱怨类型参数是“隐藏的”并且是建议您通过编写 args 来提供 ArgMatches<'_> 的完整类型,以使您的代码更加明确和清晰。