fn main() {
let s1 = String::from("hello");
let closure = move || {
let s2 = s1.clone();
};
println!("{}", s1);
}
This will give an error bcs the s1 ownership goes to the closure. One way we can fix this is by cloning s1 before it enters the closure. But this will become hard to read if I got many closure that needs that data.
My question, is there any work around to make the cloning process happens inside the closure? or is there something like clone || {} available?
Compiling playground v0.0.1 (/playground)
error[E0505]: cannot move out of `s1` because it is borrowed
--> src/main.rs:11:13
|
7 | let closure = || {
| -- borrow of `s1` occurs here
8 | let s2 = s1.clone();
| -- borrow occurs due to use in closure
...
11 | consume(s1);
| ^^ move out of `s1` occurs here
12 | closure();
| ------- borrow later used here
For more information about this error, try `rustc --explain E0505`.
error: could not compile `playground` due to previous error