【问题标题】:error[E0106]: missing lifetime specifier (despite it being set) [duplicate]错误[E0106]:缺少生命周期说明符(尽管已设置)[重复]
【发布时间】:2019-01-16 13:05:29
【问题描述】:

考虑以下代码:

extern crate clap;
use clap::{App};

use std::io;

fn parse_argv() -> &'static clap::ArgMatches {
    return App::new("example")
    .get_matches()
}

fn main() -> io::Result<()> {

    let matches = parse_argv();

    Ok(())
}

这是错误:

error[E0106]: missing lifetime specifier                                                                                                                                                                                                                                                                                                                                   
 --> src/main.rs:6:29                                                                                                                                                                                                                                                                                                                                                      
  |                                                                                                                                                                                                                                                                                                                                                                        
6 | fn parse_argv() -> &'static clap::ArgMatches {                                                                                                                                                                                                                                                                                                                         
  |                             ^^^^^^^^^^^^^^^^ help: consider giving it a 'static lifetime: `clap::ArgMatches + 'static`                                                                                                                                                                                                                                                 
  |                                                                                                                                                                                                                                                                                                                                                                        
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from

这里有什么问题,我该如何解决?我想我做了编译器要求的,但错误不会消失。

【问题讨论】:

    标签: rust borrow-checker


    【解决方案1】:

    我在#rust-beginners IRC 频道上得到了答案:

    13:10:17 拖拉机 | d33tah:我怀疑 ArgMatches 实际上 想要生命周期,所以 ArgMatches 在这种情况下

    因此,解决方案是将函数签名更改为:

    fn parse_argv() -> clap::ArgMatches<'static> {
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      相关资源
      最近更新 更多