error[E0106]: missing lifetime specifier (despite it being set) [duplicate]

早过忘川 提交于 2021-02-11 00:20:46

问题


Consider the following code:

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(())
}

This is the error:

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

What is the problem here and how do I solve it? I think I did what compiler asked for, but the error won't disappear.


回答1:


I got an answer on #rust-beginners IRC channel:

13:10:17 drager | d33tah: I suspect that ArgMatches actually wants the lifetime, so ArgMatches<'static> in this case

So, the solution was to change the function signature to:

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


来源:https://stackoverflow.com/questions/54217705/errore0106-missing-lifetime-specifier-despite-it-being-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!