错误[E0106]:缺少生存期说明符(尽管已设置)

时间:2019-01-16 13:05:29

标签: rust borrow-checker

考虑以下代码:

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

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

1 个答案:

答案 0 :(得分:2)

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

  

13:10:17拖动器| d33tah:我怀疑ArgMatches实际上   想要生命周期,因此在这种情况下,ArgMatches <'static>

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

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