Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Struct 如何在结构中存储clap::ArgMatches?_Struct_Rust_Lifetime_Borrow Checker_Borrowing - Fatal编程技术网

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

Struct 如何在结构中存储clap::ArgMatches?,struct,rust,lifetime,borrow-checker,borrowing,Struct,Rust,Lifetime,Borrow Checker,Borrowing,我正在尝试将clap::ArgMatches存储在如下结构中: struct Configurator { cli_args: ArgMatches, root_directory: String } 我收到的错误是: 错误[E0106]:缺少生存期说明符 -->src/configurator.rs:5:15 | 5 | cli|u args:ArgMatches, |^^^^^^^^^^^^^^应为命名生存期参数 | 帮助:考虑引入命名的生命周期参数 | 4 |结构配置器,

我正在尝试将
clap::ArgMatches
存储在如下结构中:

struct Configurator {
    cli_args: ArgMatches,
    root_directory: String
}
我收到的错误是:

错误[E0106]:缺少生存期说明符
-->src/configurator.rs:5:15
|
5 | cli|u args:ArgMatches,
|^^^^^^^^^^^^^^应为命名生存期参数
|
帮助:考虑引入命名的生命周期参数
|
4 |结构配置器,
|
我尝试了错误输出中给出的建议解决方案,但这似乎会导致不同的错误

这里有更多的上下文:

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;
    }
}
extern板条箱clap;
使用clap::{Arg,App,ArgMatches};
结构配置器{
cli_args:ArgMatches,
根目录:String
}
impl配置程序{
pub fn build()->Configurator{
设configurator=configurator{};
//显然,这是一场比赛
让cli_args=App::new(“Rust Web服务器”)
.版本(“0.0.1”)
.作者(“Blaine Lafreniere”)
.about(“内置rust的简单web服务器”)
.arg(arg::带有_名称(“根目录”)
.short(“r”)
.long(“根目录”)
.value\u name(“根目录”)
.help(“设置web服务器将从中提供服务的根目录。”)
.takes_值(true))
.get_matches();
configurator.cli_args=cli_args;
}
}

如错误消息所示,您必须将命名的生存期说明符添加到
配置程序中。这是因为
ArgMatches
保存对值的引用,因此必须告诉Rust这些引用的有效期:

struct配置程序,
根目录:String
}
必须对
impl
块执行相同的操作:

impl{

pub fn build()->Configurator Configurator如错误消息所示,您必须将命名的生存期说明符添加到
Configurator
。这是因为
ArgMatches
保存对值的引用,因此您必须告诉Rust这些引用的有效期:

struct配置程序,
根目录:String
}
必须对
impl
块执行相同的操作:

impl{

pub fn build()->Configurator Configurator由于只使用
'static
字符串构建
应用程序
,因此
.arg\u matches()的返回类型是
ArgMatches,因为只使用
'static
字符串构建
应用程序
,然后使用
.arg\u matches()的返回类型
is
argmatches似乎您的问题可以用的答案来回答。如果没有,请您的问题解释差异。否则,我们可以将此问题标记为已回答。@Shepmaster尝试了该链接中建议的解决方案,但没有,它无法解决问题。请更新您的答案,以显示您是如何回答的构造
clap::App
以及您迄今为止所尝试的内容?只是一个精度:
App::new()
,它返回一个
ArgMatches
实例。按照错误消息的建议添加生存期说明符似乎效果很好。您能解释什么不适合您吗?您的问题似乎可以由的答案来回答。如果不是,请您的问题解释差异。否则,我们可能会k这个问题已经回答。@Shepmaster尝试了该链接中建议的解决方案,但没有,它无法解决问题。请更新您的答案,以显示您是如何构建
clap::App
的,以及您迄今为止尝试了什么?只是一个精确的答案:
App::new()
返回一个
App
struct;然后您可以使用
您的\u App\u实例解析实际的cli参数。get\u matches()
,它返回一个
ArgMatches
实例。按照错误消息的建议添加生存期说明符似乎效果很好。您能解释一下什么不适合您吗?现在更有意义了,谢谢!现在更有意义了,谢谢!