首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在结构中存储clap::ArgMatches?

如何在结构中存储clap::ArgMatches?
EN

Stack Overflow用户
提问于 2020-12-24 01:46:31
回答 2查看 258关注 0票数 1

我尝试将clap::ArgMatches存储在一个结构中,如下所示:

代码语言:javascript
复制
struct Configurator {
    cli_args: ArgMatches,
    root_directory: String
}

我收到的错误是:

代码语言:javascript
复制
error[E0106]: missing lifetime specifier
 --> src/configurator.rs:5:15
  |
5 |     cli_args: ArgMatches,
  |               ^^^^^^^^^^ expected named lifetime parameter
  |
help: consider introducing a named lifetime parameter
  |
4 | struct Configurator<'a> {
5 |     cli_args: ArgMatches<'a>,
  |

我尝试了错误输出中给出的建议解决方案,但这似乎导致了不同的错误。

下面是更多的上下文:

代码语言:javascript
复制
extern crate clap;
use clap::{Arg, App, ArgMatches};

struct Configurator {
    cli_args: ArgMatches,
    root_directory: String
}

impl Configurator {
    pub fn build() -> Configurator {
        let configurator = Configurator {};

        // returns ArgMatches apparently 
        let cli_args = App::new("Rust Web Server")
            .version("0.0.1")
            .author("Blaine Lafreniere <brlafreniere@gmail.com>")
            .about("A simple web server built in rust.")
            .arg(Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true))
            .get_matches();
        
        configurator.cli_args = cli_args;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-12-24 02:18:38

如错误消息所示,您必须向Configurator添加命名生命周期说明符。这是因为ArgMatches持有对值的引用,所以您必须告诉Rust这些引用将存在多长时间:

代码语言:javascript
复制
struct Configurator<'a> {
    cli_args: ArgMatches<'a>,
    root_directory: String
}

您必须对impl块执行相同的操作:

代码语言:javascript
复制
impl<'a> Configurator<'a> {
    pub fn build() -> Configurator<'a> {
      // ...
    }
}

代码的另一个问题是,在Rust中,实例化结构时必须初始化所有字段:

代码语言:javascript
复制
// this doesn't work
let config = Configurator {}

// but this does
let config = Configurator {
  cli_args: cli_args, 
  root_directory: "/home".to_string(),
};

最后,您必须从函数返回Configurator

代码语言:javascript
复制
pub fn build() -> Configurator<'a> {
    let cli_args = App::new("Rust Web Server")
        .version("0.0.1")
        .author("Blaine Lafreniere <brlafreniere@gmail.com>")
        .about("A simple web server built in rust.")
        .arg(
            Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true),
        )
        .get_matches();

    return Configurator {
        cli_args: cli_args,
        root_directory: "/home".to_string(),
    };
}

这是一个runnable example

票数 2
EN

Stack Overflow用户

发布于 2020-12-24 02:20:10

因为您只使用'static字符串构建App,所以.arg_matches()的返回类型是ArgMatches<'static>,您应该在Configurator结构定义中显式声明它。这比使用像'a这样的通用生命周期参数要好得多,因为它不会用任何生命周期注解“感染”您的Configurator结构。此外,在Rust中不能“部分”初始化结构,它们必须完全初始化,所以当所有数据都准备就绪时,我将Configurator结构的构造移到了build函数的底部。

代码语言:javascript
复制
use clap::{Arg, App, ArgMatches};

struct Configurator {
    cli_args: ArgMatches<'static>,
    root_directory: String
}

impl Configurator {
    pub fn build(root_directory: String) -> Configurator {
        let cli_args = App::new("Rust Web Server")
            .version("0.0.1")
            .author("Blaine Lafreniere <brlafreniere@gmail.com>")
            .about("A simple web server built in rust.")
            .arg(Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true))
            .get_matches();
        
        Configurator {
            cli_args,
            root_directory,
        }
    }
}

playground

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65428674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档