I'm trying to understand what's happening with moving/borrowing here.
fn main() {
let arg_set: HashSet<&'static str> = ["calvin",
"hobbes"].iter().cloned().collect();
let args: Vec<String> = env::args().collect();
if args.len() == 1 {
panic!("usage: comics calvin|hobbes")
} else {
assert!(args.iter().all(|&x| arg_set.contains(x.as_str())));
}
println!("Made it to the end!");
}
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=470217d9343a00615fcc56ca47044f39
The error I'm recieving:
| assert!(args.iter().all(|&x| arg_set.contains(x.as_str())));
| ^-
| ||
| |data moved here
| |move occurs because `x` has type `String`, which does not implement the `Copy` trait
| help: consider removing the `&`: `x`
Here is my understanding. args.iter()
returns an immutable iterator on the vector. Then all() take a mutable reference to the the iterator(?). My understanding really falls apart here. In the closure, I attempt to borrow x
which is the iterator that points to String
? Or is x
a actually a String
? I know because String
holds data on the heap, Moving is the default. However, my thought with |&x|
is that we'd be borrowing String
in the closure?