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

问题描述

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: `<'_>`

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

标签: rustreferencelifetimeborrow-checkerborrowing

解决方案


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


推荐阅读