Even after reading the chapters about reference ownership and borrowing, I cannot understand some things in the following code, effectively stopping me from calling more than one method from 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()
);
}
Compiling this code leads to:
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
- If I understand correctly,
app.get_matches()
asks to borrow the ownership, thusapp
must bemut
. Where does the ownership go once the function returns? - I thought
app
would still have ownership of the object, yet the compiler has a different opinion.
How can I get the matches, and effectively still call another method, such as print_help
on app
then?