我正在学习 rust 并想更好地了解这里发生的事情。我声明了以下内容:
pub struct SomeHandler {}
impl SomeHandler {
pub fn configure(&self, app: &App) {
app.subcommand(
SubCommand::with_name("server-status")
.about("Displays the status of the server")
);
}
pub fn check_match(&self, matches: &ArgMatches) -> bool {
matches.subcommand_matches("server-status").is_some()
}
pub fn handle(&self, arg_match: &ArgMatches) {
...
}
}
然后在main.rs
我这样称呼它:
let mut app = App::new("My CLI")
.author("Author <author@email.com>")
.version("0.1.0")
.about("Command line interface app");
let myhandler = SomeHandler {};
myhandler.configure(&app);
let matches = app.get_matches();
let matched = myhandler.check_match(&matches);
if !matched {
eprintln!("None of the commands match the provided args.");
process::exit(1);
}
myhandler.handle(&matches);
process::exit(0);
但我收到以下错误:
error[E0507]: cannot move out of `*app` which is behind a shared reference
--> cli\src\some_handler.rs:15:9
|
15 | app.subcommand(
| ^^^ move occurs because `*app` has type `App<'_, '_>`, which does not implement the `Copy` trait
如何修复此错误?有没有更好的方法来处理这个?我正在尝试使用多个命令和选项在 rust 中构建一个命令行应用程序。我不想在一个文件中实现它。这里有什么好的模式可以遵循?
任何帮助都会很棒,
谢谢,曼森