Guys, I cannot understand why the first example errors but the second example doesn't. Semantically they should be identical:
//First example
let args = Args::parse();
MyServer::new(move || {
let mut cfg = Config::new();
cfg.host = Some(args.host);
}
error[E0507]: cannot move out of `args.host`, a captured variable in an `Fn` closure
--> src/main.rs:37:25
|
33 | let args = Args::parse();
| ---- captured outer variable
34 |
35 | MyServer::new(move || {
| ------- captured by this `Fn` closure
36 | let mut cfg = Config::new();
37 | cfg.host = Some(args.host);
| ^^^^^^^^^^^^ move occurs because `args.host` has type `std::string::String`, which does not implement the `Copy` trait
And this^^^ is "somewhat" logical to me.
//Second example
MyServer::new(move || {
let args = Args::parse();//here I moved the args var inside the clousure
let mut cfg = Config::new();
cfg.host = Some(args.host);
}
This works, but to me this example is semantically identicall to the previous one. Here I manually moved the args variable inside the clousure where in the previous example I used move keyword to do that for me.
Can somebody explain what is happening here?
Thank you