rust - 如何解决 "indicate anonymous lifetime <' _>”错误?

标签 rust reference lifetime borrow-checker borrowing

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

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

最佳答案

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

关于rust - 如何解决 "indicate anonymous lifetime <' _>”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66171697/

相关文章:

c++ - “std::string&”类型的非常量引用的无效初始化

rust - 如何将一个字段的生命周期指定为其他字段的组合?

rust - 为什么在闭包内克隆数据不能防止错误 "closure may outlive the current function"?

rust - 在 Rust 中推送运行时操作以编译时间?

java - 我可以在 Java 中获取当前引用的名称吗?

logging - 如何使用 log4rs 创建自定义过滤器类型?

c++ - 什么是悬空引用?

reference - 有没有办法返回对函数中创建的变量的引用?

linux - 使用 Cargo 构建 openssl crate 的问题

rust - 如何定义接受任何实现默认特征的参数的函数