gpt4 book ai didi

rust - clap::App 多次移动所有权的方法调用

转载 作者:行者123 更新时间:2023-11-29 08:08:43 24 4
gpt4 key购买 nike

即使阅读了关于引用所有权和借用的章节,我仍然无法理解以下代码中的某些内容,这实际上阻止了我从 clap::App 调用多个方法!

extern crate clap;

use clap::App;

fn main() {
let mut app =
App::new("name me").args_from_usage("<input_file> 'Sets the input file to use'");
let matches = app.get_matches();
app.print_help();
println!(
"Using input file: {}",
matches.value_of("input_file").unwrap()
);
}

编译这段代码会导致:

error[E0382]: use of moved value: `app`
--> src/main.rs:9:5
|
8 | let matches = app.get_matches();
| --- value moved here
9 | app.print_help();
| ^^^ value used here after move
|
= note: move occurs because `app` has type `clap::App<'_, '_>`, which does not implement the `Copy` trait
  1. 如果我理解正确,app.get_matches() 要求借用所有权,因此 app 必须是 mut。一旦函数返回,所有权会去哪里?
  2. 我认为 app 仍然拥有该对象的所有权,但编译器有不同的意见。

我怎样才能获得匹配项,并有效地调用另一个方法,例如 app 上的 print_help 呢?

最佳答案

阅读function signature for App::get_matches :

fn get_matches(self) -> ArgMatches<'a>

这根据值获取 self,也可以说是 消耗 值;之后您不能调用它的任何方法。对此无能为力;想必作者对此有充分的理由。

现在回顾App::print_help :

fn print_help(&mut self) -> ClapResult<()>

它需要一个引用(恰好是可变的)。您无需转移所有权即可调用此方法。


If I understand correctly, app.get_matches() asks to borrow the ownership, thus app must be mut. Where does the ownership go once the function returns?

你没有理解正确,多维度的。

  1. get_matches 消耗值,它不借用任何东西。
  2. 借用的值不需要可变。
  3. 当您确实借了东西时,所有权不会“转移”到任何地方。原主人继续拥有它。这就是为什么它被称为借用

How can I get the matches, and effectively still call another method, such as print_help on app then?

你不知道。显而易见的解决方法是克隆原始对象,产生第二个值。然后您可以使用一个值并仍然调用第二个值的方法。


基本上,听起来您正在尝试做图书馆不鼓励您做的事情。也许您应该重新评估您的目标和/或查看库的预期用途。例如,get_matches 会在用户请求时自动显示帮助文本,那么您的代码为什么要尝试这样做呢?

来自Clap issue tracker :

You have a few options. You can either use AppSettings::ArgRequiredElseHelp or you can keep the move from happening by using App::get_matches_from_safe_borrow.

关于rust - clap::App 多次移动所有权的方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com