相关疑难解决方法(0)

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

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

ownership rust clap

6
推荐指数
1
解决办法
1580
查看次数

标签 统计

clap ×1

ownership ×1

rust ×1