即使在阅读了有关引用所有权和借用的章节之后,我仍然无法理解以下代码中的某些内容,这有效地阻止了我从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()
);
}
Run Code Online (Sandbox Code Playgroud)
编译此代码会导致:
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()
);
}
Run Code Online (Sandbox Code Playgroud)
app.get_matches()要求借用所有权,那么app一定是mut。一旦函数返回,所有权会去哪里?app仍然拥有该对象的所有权,但编译器有不同的意见。 …