1

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.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
d33tah
  • 10,999
  • 13
  • 68
  • 158

1 Answers1

5

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> {
d33tah
  • 10,999
  • 13
  • 68
  • 158