[英]How do I store clap::ArgMatches in a struct?
我正在尝试将clap::ArgMatches
存储在这样的结构中:
struct Configurator {
cli_args: ArgMatches,
root_directory: String
}
我收到的错误是:
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>,
|
我尝试了错误 output 中给出的建议解决方案,但这似乎会导致不同的错误。
这里有更多的上下文:
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;
}
}
如错误消息所示,您必须将命名的生命周期说明符添加到Configurator
。 这是因为ArgMatches
持有对值的引用,因此您必须告诉 Rust 这些引用将存在多长时间:
struct Configurator<'a> {
cli_args: ArgMatches<'a>,
root_directory: String
}
您必须对impl
块执行相同的操作:
impl<'a> Configurator<'a> {
pub fn build() -> Configurator<'a> {
// ...
}
}
您的代码的另一个问题是,在 Rust 中,您必须在实例化结构时初始化所有字段:
// this doesn't work
let config = Configurator {}
// but this does
let config = Configurator {
cli_args: cli_args,
root_directory: "/home".to_string(),
};
最后,您必须从 function 返回Configurator
器:
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(),
};
}
这是一个可运行的例子。
由于您仅使用'static
字符串构建App
, .arg_matches()
的返回类型是ArgMatches<'static>
,您应该在Configurator
结构定义中明确 state 。 这比使用像'a
这样的通用生命周期参数要好得多,因为它不会用任何生命周期注释“感染”您的Configurator
结构。 此外,您不能“部分”初始化 Rust 中的结构,它们必须完全初始化,因此当所有数据准备就绪时,我已将Configurator
结构的构造移至build
function 的底部。
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,
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.