1 #### Note: this error code is no longer emitted by the compiler.
3 This error occurs when an attempt is made to move a borrowed variable into a
6 Erroneous code example:
14 let fancy_num = FancyNum { num: 5 };
15 let fancy_ref = &fancy_num;
18 println!("child function: {}", fancy_num.num);
19 // error: cannot move `fancy_num` into closure because it is borrowed
23 println!("main function: {}", fancy_ref.num);
27 Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into
28 the closure `x`. There is no way to move a value into a closure while it is
29 borrowed, as that would invalidate the borrow.
31 If the closure can't outlive the value being moved, try using a reference
40 let fancy_num = FancyNum { num: 5 };
41 let fancy_ref = &fancy_num;
44 // fancy_ref is usable here because it doesn't move `fancy_num`
45 println!("child function: {}", fancy_ref.num);
50 println!("main function: {}", fancy_num.num);
54 If the value has to be borrowed and then moved, try limiting the lifetime of
55 the borrow using a scoped block:
63 let fancy_num = FancyNum { num: 5 };
66 let fancy_ref = &fancy_num;
67 println!("main function: {}", fancy_ref.num);
68 // `fancy_ref` goes out of scope here
72 // `fancy_num` can be moved now (no more references exist)
73 println!("child function: {}", fancy_num.num);
80 If the lifetime of a reference isn't enough, such as in the case of threading,
81 consider using an `Arc` to create a reference-counted value:
92 let fancy_ref1 = Arc::new(FancyNum { num: 5 });
93 let fancy_ref2 = fancy_ref1.clone();
95 let x = thread::spawn(move || {
96 // `fancy_ref1` can be moved and has a `'static` lifetime
97 println!("child thread: {}", fancy_ref1.num);
100 x.join().expect("child thread should finish");
101 println!("main thread: {}", fancy_ref2.num);