1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_snake_case)]
13 register_long_diagnostics! {
16 The value of statics and constants must be known at compile time, and they live
17 for the entire lifetime of a program. Creating a boxed value allocates memory on
18 the heap at runtime, and therefore cannot be done at compile time. Erroneous
22 #![feature(box_syntax)]
24 const CON : Box<i32> = box 0;
29 Static and const variables can refer to other const variables. But a const
30 variable cannot refer to a static variable. For example, `Y` cannot refer to
38 To fix this, the value can be extracted as a const and then used:
47 // FIXME(#24111) Change the language here when const fn stabilizes
49 The only functions that can be called in static or constant expressions are
50 `const` functions, and struct/enum constructors. `const` functions are only
51 available on a nightly compiler. Rust currently does not support more general
52 compile-time function execution.
55 const FOO: Option<u8> = Some(1); // enum constructor
57 const BAR: Bar = Bar {x: 1}; // struct constructor
60 See [RFC 911] for more details on the design of `const fn`s.
62 [RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
66 Blocks in constants may only contain items (such as constant, function
67 definition, etc...) and a tail expression. Erroneous code example:
70 const FOO: i32 = { let x = 0; x }; // 'x' isn't an item!
73 To avoid it, you have to replace the non-item object:
76 const FOO: i32 = { const X : i32 = 0; X };
81 References in statics and constants may only refer to immutable values.
82 Erroneous code example:
88 // these three are not allowed:
89 const CR: &'static mut i32 = &mut C;
90 static STATIC_REF: &'static mut i32 = &mut X;
91 static CONST_REF: &'static mut i32 = &mut C;
94 Statics are shared everywhere, and if they refer to mutable data one might
95 violate memory safety since holding multiple mutable references to shared data
98 If you really want global mutable state, try using `static mut` or a global
104 The value of static and constant integers must be known at compile time. You
105 can't cast a pointer to an integer because the address of a pointer can
108 For example, if you write:
110 ```compile_fail,E0018
111 static MY_STATIC: u32 = 42;
112 static MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize;
113 static WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR;
116 Then `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However,
117 the address can change when the program is linked, as well as change
118 between different executions due to ASLR, and many linkers would
119 not be able to calculate the value of `WHAT`.
121 On the other hand, static and constant pointers can point either to
122 a known numeric address or to the address of a symbol.
125 static MY_STATIC: u32 = 42;
126 static MY_STATIC_ADDR: &'static u32 = &MY_STATIC;
127 const CONST_ADDR: *const u8 = 0x5f3759df as *const u8;
130 This does not pose a problem by itself because they can't be
135 A function call isn't allowed in the const's initialization expression
136 because the expression's value must be known at compile-time. Erroneous code
145 fn test(&self) -> i32 {
151 const FOO: Test = Test::V1;
153 const A: i32 = FOO.test(); // You can't call Test::func() here!
157 Remember: you can't use a function call inside a const's initialization
158 expression! However, you can totally use it anywhere else:
166 fn func(&self) -> i32 {
172 const FOO: Test = Test::V1;
174 FOO.func(); // here is good
175 let x = FOO.func(); // or even here!
181 Constant functions are not allowed to mutate anything. Thus, binding to an
182 argument with a mutable pattern is not allowed. For example,
185 const fn foo(mut x: u8) {
190 Is incorrect because the function body may not mutate `x`.
192 Remove any mutable bindings from the argument list to fix this error. In case
193 you need to mutate the argument, try lazily initializing a global variable
194 instead of using a `const fn`, or refactoring the code to a functional style to
195 avoid mutation if possible.
199 Unsafe code was used outside of an unsafe function or block.
201 Erroneous code example:
203 ```compile_fail,E0133
204 unsafe fn f() { return; } // This is the unsafe code
207 f(); // error: call to unsafe function requires unsafe function or block
211 Using unsafe functionality is potentially dangerous and disallowed by safety
214 * Dereferencing raw pointers
215 * Calling functions via FFI
216 * Calling functions marked unsafe
218 These safety checks can be relaxed for a section of the code by wrapping the
219 unsafe instructions with an `unsafe` block. For instance:
222 unsafe fn f() { return; }
225 unsafe { f(); } // ok!
229 See also https://doc.rust-lang.org/book/first-edition/unsafe.html
233 This error occurs when an attempt is made to use data captured by a closure,
234 when that data may no longer exist. It's most commonly seen when attempting to
237 ```compile_fail,E0373
238 fn foo() -> Box<Fn(u32) -> u32> {
244 Notice that `x` is stack-allocated by `foo()`. By default, Rust captures
245 closed-over data by reference. This means that once `foo()` returns, `x` no
246 longer exists. An attempt to access `x` within the closure would thus be
249 Another situation where this might be encountered is when spawning threads:
251 ```compile_fail,E0373
256 let thr = std::thread::spawn(|| {
262 Since our new thread runs in parallel, the stack frame containing `x` and `y`
263 may well have disappeared by the time we try to use them. Even if we call
264 `thr.join()` within foo (which blocks until `thr` has completed, ensuring the
265 stack frame won't disappear), we will not succeed: the compiler cannot prove
266 that this behaviour is safe, and so won't let us do it.
268 The solution to this problem is usually to switch to using a `move` closure.
269 This approach moves (or copies, where possible) data into the closure, rather
270 than taking references to it. For example:
273 fn foo() -> Box<Fn(u32) -> u32> {
275 Box::new(move |y| x + y)
279 Now that the closure has its own copy of the data, there's no need to worry
284 It is not allowed to use or capture an uninitialized variable. For example:
286 ```compile_fail,E0381
289 let y = x; // error, use of possibly uninitialized variable
293 To fix this, ensure that any declared variables are initialized before being
305 This error occurs when an attempt is made to use a variable after its contents
306 have been moved elsewhere. For example:
308 ```compile_fail,E0382
309 struct MyStruct { s: u32 }
312 let mut x = MyStruct{ s: 5u32 };
319 Since `MyStruct` is a type that is not marked `Copy`, the data gets moved out
320 of `x` when we set `y`. This is fundamental to Rust's ownership system: outside
321 of workarounds like `Rc`, a value cannot be owned by more than one variable.
323 Sometimes we don't need to move the value. Using a reference, we can let another
324 function borrow the value without changing its ownership. In the example below,
325 we don't actually have to move our string to `calculate_length`, we can give it
326 a reference to it with `&` instead.
330 let s1 = String::from("hello");
332 let len = calculate_length(&s1);
334 println!("The length of '{}' is {}.", s1, len);
337 fn calculate_length(s: &String) -> usize {
342 A mutable reference can be created with `&mut`.
344 Sometimes we don't want a reference, but a duplicate. All types marked `Clone`
345 can be duplicated by calling `.clone()`. Subsequent changes to a clone do not
346 affect the original variable.
348 Most types in the standard library are marked `Clone`. The example below
349 demonstrates using `clone()` on a string. `s1` is first set to "many", and then
350 copied to `s2`. Then the first character of `s1` is removed, without affecting
351 `s2`. "any many" is printed to the console.
355 let mut s1 = String::from("many");
358 println!("{} {}", s1, s2);
362 If we control the definition of a type, we can implement `Clone` on it ourselves
363 with `#[derive(Clone)]`.
365 Some types have no ownership semantics at all and are trivial to duplicate. An
366 example is `i32` and the other number types. We don't have to call `.clone()` to
367 clone them, because they are marked `Copy` in addition to `Clone`. Implicit
368 cloning is more convienient in this case. We can mark our own types `Copy` if
369 all their members also are marked `Copy`.
371 In the example below, we implement a `Point` type. Because it only stores two
372 integers, we opt-out of ownership semantics with `Copy`. Then we can
373 `let p2 = p1` without `p1` being moved.
376 #[derive(Copy, Clone)]
377 struct Point { x: i32, y: i32 }
380 let mut p1 = Point{ x: -1, y: 2 };
383 println!("p1: {}, {}", p1.x, p1.y);
384 println!("p2: {}, {}", p2.x, p2.y);
388 Alternatively, if we don't control the struct's definition, or mutable shared
389 ownership is truly required, we can use `Rc` and `RefCell`:
392 use std::cell::RefCell;
395 struct MyStruct { s: u32 }
398 let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
400 x.borrow_mut().s = 6;
401 println!("{}", x.borrow().s);
405 With this approach, x and y share ownership of the data via the `Rc` (reference
406 count type). `RefCell` essentially performs runtime borrow checking: ensuring
407 that at most one writer or multiple readers can access the data at any one time.
409 If you wish to learn more about ownership in Rust, start with the chapter in the
412 https://doc.rust-lang.org/book/first-edition/ownership.html
416 This error occurs when an attempt is made to partially reinitialize a
417 structure that is currently uninitialized.
419 For example, this can happen when a drop has taken place:
421 ```compile_fail,E0383
426 fn drop(&mut self) { /* ... */ }
429 let mut x = Foo { a: 1 };
430 drop(x); // `x` is now uninitialized
431 x.a = 2; // error, partial reinitialization of uninitialized structure `t`
434 This error can be fixed by fully reinitializing the structure in question:
441 fn drop(&mut self) { /* ... */ }
444 let mut x = Foo { a: 1 };
451 This error occurs when an attempt is made to reassign an immutable variable.
454 ```compile_fail,E0384
457 x = 5; // error, reassignment of immutable variable
461 By default, variables in Rust are immutable. To fix this error, add the keyword
462 `mut` after the keyword `let` when declaring the variable. For example:
473 This error occurs when an attempt is made to mutate the target of a mutable
474 reference stored inside an immutable container.
476 For example, this can happen when storing a `&mut` inside an immutable `Box`:
478 ```compile_fail,E0386
480 let y: Box<_> = Box::new(&mut x);
481 **y = 2; // error, cannot assign to data in an immutable container
484 This error can be fixed by making the container mutable:
488 let mut y: Box<_> = Box::new(&mut x);
492 It can also be fixed by using a type with interior mutability, such as `Cell`
499 let y: Box<Cell<_>> = Box::new(Cell::new(x));
505 This error occurs when an attempt is made to mutate or mutably reference data
506 that a closure has captured immutably. Examples of this error are shown below:
508 ```compile_fail,E0387
509 // Accepts a function or a closure that captures its environment immutably.
510 // Closures passed to foo will not be able to mutate their closed-over state.
511 fn foo<F: Fn()>(f: F) { }
513 // Attempts to mutate closed-over data. Error message reads:
514 // `cannot assign to data in a captured outer variable...`
520 // Attempts to take a mutable reference to closed-over data. Error message
521 // reads: `cannot borrow data mutably in a captured outer variable...`
524 foo(|| { let y = &mut x; });
528 The problem here is that foo is defined as accepting a parameter of type `Fn`.
529 Closures passed into foo will thus be inferred to be of type `Fn`, meaning that
530 they capture their context immutably.
532 If the definition of `foo` is under your control, the simplest solution is to
533 capture the data mutably. This can be done by defining `foo` to take FnMut
537 fn foo<F: FnMut()>(f: F) { }
540 Alternatively, we can consider using the `Cell` and `RefCell` types to achieve
541 interior mutability through a shared reference. Our example's `mutable`
542 function could be redefined as below:
547 fn foo<F: Fn()>(f: F) { }
550 let x = Cell::new(0u32);
555 You can read more about cell types in the API documentation:
557 https://doc.rust-lang.org/std/cell/
561 E0388 was removed and is no longer issued.
565 An attempt was made to mutate data using a non-mutable reference. This
566 commonly occurs when attempting to assign to a non-mutable reference of a
567 mutable reference (`&(&mut T)`).
569 Example of erroneous code:
571 ```compile_fail,E0389
577 let mut fancy = FancyNum{ num: 5 };
578 let fancy_ref = &(&mut fancy);
579 fancy_ref.num = 6; // error: cannot assign to data in a `&` reference
580 println!("{}", fancy_ref.num);
584 Here, `&mut fancy` is mutable, but `&(&mut fancy)` is not. Creating an
585 immutable reference to a value borrows it immutably. There can be multiple
586 references of type `&(&mut T)` that point to the same value, so they must be
587 immutable to prevent multiple mutable references to the same value.
589 To fix this, either remove the outer reference:
597 let mut fancy = FancyNum{ num: 5 };
599 let fancy_ref = &mut fancy;
600 // `fancy_ref` is now &mut FancyNum, rather than &(&mut FancyNum)
602 fancy_ref.num = 6; // No error!
604 println!("{}", fancy_ref.num);
608 Or make the outer reference mutable:
616 let mut fancy = FancyNum{ num: 5 };
618 let fancy_ref = &mut (&mut fancy);
619 // `fancy_ref` is now &mut(&mut FancyNum), rather than &(&mut FancyNum)
621 fancy_ref.num = 6; // No error!
623 println!("{}", fancy_ref.num);
629 A static was referred to by value by another static.
631 Erroneous code examples:
633 ```compile_fail,E0394
635 static B: u32 = A; // error: cannot refer to other statics by value, use the
636 // address-of operator or a constant instead
639 A static cannot be referred by value. To fix this issue, either use a
643 const A: u32 = 0; // `A` is now a constant
644 static B: u32 = A; // ok!
647 Or refer to `A` by reference:
651 static B: &'static u32 = &A; // ok!
656 The value assigned to a constant scalar must be known at compile time,
657 which is not the case when comparing raw pointers.
659 Erroneous code example:
661 ```compile_fail,E0395
662 static FOO: i32 = 42;
663 static BAR: i32 = 42;
665 static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
666 // error: raw pointers cannot be compared in statics!
669 The address assigned by the linker to `FOO` and `BAR` may or may not
670 be identical, so the value of `BAZ` can't be determined.
672 If you want to do the comparison, please do it at run-time.
677 static FOO: i32 = 42;
678 static BAR: i32 = 42;
680 let baz: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
681 // baz isn't a constant expression so it's ok
686 A value was moved. However, its size was not known at compile time, and only
687 values of a known size can be moved.
689 Erroneous code example:
692 #![feature(box_syntax)]
695 let array: &[isize] = &[1, 2, 3];
696 let _x: Box<[isize]> = box *array;
697 // error: cannot move a value of type [isize]: the size of [isize] cannot
698 // be statically determined
702 In Rust, you can only move a value when its size is known at compile time.
704 To work around this restriction, consider "hiding" the value behind a reference:
705 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
706 it around as usual. Example:
709 #![feature(box_syntax)]
712 let array: &[isize] = &[1, 2, 3];
713 let _x: Box<&[isize]> = box array; // ok!
719 The value behind a raw pointer can't be determined at compile-time
720 (or even link-time), which means it can't be used in a constant
721 expression. Erroneous code example:
723 ```compile_fail,E0396
724 const REG_ADDR: *const u8 = 0x5f3759df as *const u8;
726 const VALUE: u8 = unsafe { *REG_ADDR };
727 // error: raw pointers cannot be dereferenced in constants
730 A possible fix is to dereference your pointer at some point in run-time.
735 const REG_ADDR: *const u8 = 0x5f3759df as *const u8;
737 let reg_value = unsafe { *REG_ADDR };
742 A borrow of a constant containing interior mutability was attempted. Erroneous
745 ```compile_fail,E0492
746 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
748 const A: AtomicUsize = ATOMIC_USIZE_INIT;
749 static B: &'static AtomicUsize = &A;
750 // error: cannot borrow a constant which may contain interior mutability,
751 // create a static instead
754 A `const` represents a constant value that should never change. If one takes
755 a `&` reference to the constant, then one is taking a pointer to some memory
756 location containing the value. Normally this is perfectly fine: most values
757 can't be changed via a shared `&` pointer, but interior mutability would allow
758 it. That is, a constant value could be mutated. On the other hand, a `static` is
759 explicitly a single memory location, which can be mutated at will.
761 So, in order to solve this error, either use statics which are `Sync`:
764 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
766 static A: AtomicUsize = ATOMIC_USIZE_INIT;
767 static B: &'static AtomicUsize = &A; // ok!
770 You can also have this error while using a cell type:
772 ```compile_fail,E0492
773 #![feature(const_cell_new)]
777 const A: Cell<usize> = Cell::new(1);
778 const B: &'static Cell<usize> = &A;
779 // error: cannot borrow a constant which may contain interior mutability,
780 // create a static instead
783 struct C { a: Cell<usize> }
785 const D: C = C { a: Cell::new(1) };
786 const E: &'static Cell<usize> = &D.a; // error
789 const F: &'static C = &D; // error
792 This is because cell types do operations that are not thread-safe. Due to this,
793 they don't implement Sync and thus can't be placed in statics. In this
794 case, `StaticMutex` would work just fine, but it isn't stable yet:
795 https://doc.rust-lang.org/nightly/std/sync/struct.StaticMutex.html
797 However, if you still wish to use these types, you can achieve this by an unsafe
801 #![feature(const_cell_new)]
804 use std::marker::Sync;
806 struct NotThreadSafe<T> {
810 unsafe impl<T> Sync for NotThreadSafe<T> {}
812 static A: NotThreadSafe<usize> = NotThreadSafe { value : Cell::new(1) };
813 static B: &'static NotThreadSafe<usize> = &A; // ok!
816 Remember this solution is unsafe! You will have to ensure that accesses to the
817 cell are synchronized.
821 A reference of an interior static was assigned to another const/static.
822 Erroneous code example:
824 ```compile_fail,E0494
829 static S : Foo = Foo { a : 0 };
830 static A : &'static u32 = &S.a;
831 // error: cannot refer to the interior of another static, use a
835 The "base" variable has to be a const if you want another static/const variable
836 to refer to one of its fields. Example:
843 const S : Foo = Foo { a : 0 };
844 static A : &'static u32 = &S.a; // ok!
849 A variable was borrowed as mutable more than once. Erroneous code example:
851 ```compile_fail,E0499
855 // error: cannot borrow `i` as mutable more than once at a time
858 Please note that in rust, you can either have many immutable references, or one
859 mutable reference. Take a look at
860 https://doc.rust-lang.org/stable/book/references-and-borrowing.html for more
861 information. Example:
866 let mut x = &mut i; // ok!
871 let b = &i; // still ok!
872 let c = &i; // still ok!
877 A borrowed variable was used in another closure. Example of erroneous code:
880 fn you_know_nothing(jon_snow: &mut i32) {
881 let nights_watch = || {
885 *jon_snow = 3; // error: closure requires unique access to `jon_snow`
886 // but it is already borrowed
891 In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
892 cannot be borrowed by the `starks` closure at the same time. To fix this issue,
893 you can put the closure in its own scope:
896 fn you_know_nothing(jon_snow: &mut i32) {
898 let nights_watch = || {
901 } // At this point, `jon_snow` is free.
908 Or, if the type implements the `Clone` trait, you can clone it between
912 fn you_know_nothing(jon_snow: &mut i32) {
913 let mut jon_copy = jon_snow.clone();
914 let nights_watch = || {
925 This error indicates that a mutable variable is being used while it is still
926 captured by a closure. Because the closure has borrowed the variable, it is not
927 available for use until the closure goes out of scope.
929 Note that a capture will either move or borrow a variable, but in this
930 situation, the closure is borrowing the variable. Take a look at
931 http://rustbyexample.com/fn/closures/capture.html for more information about
934 Example of erroneous code:
936 ```compile_fail,E0501
937 fn inside_closure(x: &mut i32) {
938 // Actions which require unique access
941 fn outside_closure(x: &mut i32) {
942 // Actions which require unique access
945 fn foo(a: &mut i32) {
949 outside_closure(a); // error: cannot borrow `*a` as mutable because previous
950 // closure requires unique access.
954 To fix this error, you can place the closure in its own scope:
957 fn inside_closure(x: &mut i32) {}
958 fn outside_closure(x: &mut i32) {}
960 fn foo(a: &mut i32) {
965 } // borrow on `a` ends.
966 outside_closure(a); // ok!
970 Or you can pass the variable as a parameter to the closure:
973 fn inside_closure(x: &mut i32) {}
974 fn outside_closure(x: &mut i32) {}
976 fn foo(a: &mut i32) {
977 let bar = |s: &mut i32| {
985 It may be possible to define the closure later:
988 fn inside_closure(x: &mut i32) {}
989 fn outside_closure(x: &mut i32) {}
991 fn foo(a: &mut i32) {
1001 This error indicates that you are trying to borrow a variable as mutable when it
1002 has already been borrowed as immutable.
1004 Example of erroneous code:
1006 ```compile_fail,E0502
1007 fn bar(x: &mut i32) {}
1008 fn foo(a: &mut i32) {
1009 let ref y = a; // a is borrowed as immutable.
1010 bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
1015 To fix this error, ensure that you don't have any other references to the
1016 variable before trying to access it mutably:
1019 fn bar(x: &mut i32) {}
1020 fn foo(a: &mut i32) {
1022 let ref y = a; // ok!
1026 For more information on the rust ownership system, take a look at
1027 https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
1031 A value was used after it was mutably borrowed.
1033 Example of erroneous code:
1035 ```compile_fail,E0503
1038 // Create a mutable borrow of `value`. This borrow
1039 // lives until the end of this function.
1040 let _borrow = &mut value;
1041 let _sum = value + 1; // error: cannot use `value` because
1042 // it was mutably borrowed
1046 In this example, `value` is mutably borrowed by `borrow` and cannot be
1047 used to calculate `sum`. This is not possible because this would violate
1048 Rust's mutability rules.
1050 You can fix this error by limiting the scope of the borrow:
1055 // By creating a new block, you can limit the scope
1056 // of the reference.
1058 let _borrow = &mut value; // Use `_borrow` inside this block.
1060 // The block has ended and with it the borrow.
1061 // You can now use `value` again.
1062 let _sum = value + 1;
1066 Or by cloning `value` before borrowing it:
1071 // We clone `value`, creating a copy.
1072 let value_cloned = value.clone();
1073 // The mutable borrow is a reference to `value` and
1074 // not to `value_cloned`...
1075 let _borrow = &mut value;
1076 // ... which means we can still use `value_cloned`,
1077 let _sum = value_cloned + 1;
1078 // even though the borrow only ends here.
1082 You can find more information about borrowing in the rust-book:
1083 http://doc.rust-lang.org/stable/book/references-and-borrowing.html
1087 This error occurs when an attempt is made to move a borrowed variable into a
1090 Example of erroneous code:
1092 ```compile_fail,E0504
1098 let fancy_num = FancyNum { num: 5 };
1099 let fancy_ref = &fancy_num;
1102 println!("child function: {}", fancy_num.num);
1103 // error: cannot move `fancy_num` into closure because it is borrowed
1107 println!("main function: {}", fancy_ref.num);
1111 Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into
1112 the closure `x`. There is no way to move a value into a closure while it is
1113 borrowed, as that would invalidate the borrow.
1115 If the closure can't outlive the value being moved, try using a reference
1124 let fancy_num = FancyNum { num: 5 };
1125 let fancy_ref = &fancy_num;
1128 // fancy_ref is usable here because it doesn't move `fancy_num`
1129 println!("child function: {}", fancy_ref.num);
1134 println!("main function: {}", fancy_num.num);
1138 If the value has to be borrowed and then moved, try limiting the lifetime of
1139 the borrow using a scoped block:
1147 let fancy_num = FancyNum { num: 5 };
1150 let fancy_ref = &fancy_num;
1151 println!("main function: {}", fancy_ref.num);
1152 // `fancy_ref` goes out of scope here
1156 // `fancy_num` can be moved now (no more references exist)
1157 println!("child function: {}", fancy_num.num);
1164 If the lifetime of a reference isn't enough, such as in the case of threading,
1165 consider using an `Arc` to create a reference-counted value:
1176 let fancy_ref1 = Arc::new(FancyNum { num: 5 });
1177 let fancy_ref2 = fancy_ref1.clone();
1179 let x = thread::spawn(move || {
1180 // `fancy_ref1` can be moved and has a `'static` lifetime
1181 println!("child thread: {}", fancy_ref1.num);
1184 x.join().expect("child thread should finish");
1185 println!("main thread: {}", fancy_ref2.num);
1191 A value was moved out while it was still borrowed.
1193 Erroneous code example:
1195 ```compile_fail,E0505
1198 fn eat(val: Value) {}
1203 let _ref_to_val: &Value = &x;
1209 Here, the function `eat` takes the ownership of `x`. However,
1210 `x` cannot be moved because it was borrowed to `_ref_to_val`.
1211 To fix that you can do few different things:
1213 * Try to avoid moving the variable.
1214 * Release borrow before move.
1215 * Implement the `Copy` trait on the type.
1222 fn eat(val: &Value) {}
1227 let _ref_to_val: &Value = &x;
1228 eat(&x); // pass by reference, if it's possible
1238 fn eat(val: Value) {}
1243 let _ref_to_val: &Value = &x;
1245 eat(x); // release borrow and then move it.
1252 #[derive(Clone, Copy)] // implement Copy trait
1255 fn eat(val: Value) {}
1260 let _ref_to_val: &Value = &x;
1261 eat(x); // it will be copied here.
1266 You can find more information about borrowing in the rust-book:
1267 http://doc.rust-lang.org/stable/book/references-and-borrowing.html
1271 This error occurs when an attempt is made to assign to a borrowed value.
1273 Example of erroneous code:
1275 ```compile_fail,E0506
1281 let mut fancy_num = FancyNum { num: 5 };
1282 let fancy_ref = &fancy_num;
1283 fancy_num = FancyNum { num: 6 };
1284 // error: cannot assign to `fancy_num` because it is borrowed
1286 println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
1290 Because `fancy_ref` still holds a reference to `fancy_num`, `fancy_num` can't
1291 be assigned to a new value as it would invalidate the reference.
1293 Alternatively, we can move out of `fancy_num` into a second `fancy_num`:
1301 let mut fancy_num = FancyNum { num: 5 };
1302 let moved_num = fancy_num;
1303 fancy_num = FancyNum { num: 6 };
1305 println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
1309 If the value has to be borrowed, try limiting the lifetime of the borrow using
1318 let mut fancy_num = FancyNum { num: 5 };
1321 let fancy_ref = &fancy_num;
1322 println!("Ref: {}", fancy_ref.num);
1325 // Works because `fancy_ref` is no longer in scope
1326 fancy_num = FancyNum { num: 6 };
1327 println!("Num: {}", fancy_num.num);
1331 Or by moving the reference into a function:
1339 let mut fancy_num = FancyNum { num: 5 };
1341 print_fancy_ref(&fancy_num);
1343 // Works because function borrow has ended
1344 fancy_num = FancyNum { num: 6 };
1345 println!("Num: {}", fancy_num.num);
1348 fn print_fancy_ref(fancy_ref: &FancyNum){
1349 println!("Ref: {}", fancy_ref.num);
1355 You tried to move out of a value which was borrowed. Erroneous code example:
1357 ```compile_fail,E0507
1358 use std::cell::RefCell;
1360 struct TheDarkKnight;
1362 impl TheDarkKnight {
1363 fn nothing_is_true(self) {}
1367 let x = RefCell::new(TheDarkKnight);
1369 x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
1373 Here, the `nothing_is_true` method takes the ownership of `self`. However,
1374 `self` cannot be moved because `.borrow()` only provides an `&TheDarkKnight`,
1375 which is a borrow of the content owned by the `RefCell`. To fix this error,
1376 you have three choices:
1378 * Try to avoid moving the variable.
1379 * Somehow reclaim the ownership.
1380 * Implement the `Copy` trait on the type.
1385 use std::cell::RefCell;
1387 struct TheDarkKnight;
1389 impl TheDarkKnight {
1390 fn nothing_is_true(&self) {} // First case, we don't take ownership
1394 let x = RefCell::new(TheDarkKnight);
1396 x.borrow().nothing_is_true(); // ok!
1403 use std::cell::RefCell;
1405 struct TheDarkKnight;
1407 impl TheDarkKnight {
1408 fn nothing_is_true(self) {}
1412 let x = RefCell::new(TheDarkKnight);
1413 let x = x.into_inner(); // we get back ownership
1415 x.nothing_is_true(); // ok!
1422 use std::cell::RefCell;
1424 #[derive(Clone, Copy)] // we implement the Copy trait
1425 struct TheDarkKnight;
1427 impl TheDarkKnight {
1428 fn nothing_is_true(self) {}
1432 let x = RefCell::new(TheDarkKnight);
1434 x.borrow().nothing_is_true(); // ok!
1438 Moving a member out of a mutably borrowed struct will also cause E0507 error:
1440 ```compile_fail,E0507
1441 struct TheDarkKnight;
1443 impl TheDarkKnight {
1444 fn nothing_is_true(self) {}
1448 knight: TheDarkKnight
1452 let mut cave = Batcave {
1453 knight: TheDarkKnight
1455 let borrowed = &mut cave;
1457 borrowed.knight.nothing_is_true(); // E0507
1461 It is fine only if you put something back. `mem::replace` can be used for that:
1464 # struct TheDarkKnight;
1465 # impl TheDarkKnight { fn nothing_is_true(self) {} }
1466 # struct Batcave { knight: TheDarkKnight }
1469 let mut cave = Batcave {
1470 knight: TheDarkKnight
1472 let borrowed = &mut cave;
1474 mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
1477 You can find more information about borrowing in the rust-book:
1478 http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
1482 A value was moved out of a non-copy fixed-size array.
1484 Example of erroneous code:
1486 ```compile_fail,E0508
1490 let array = [NonCopy; 1];
1491 let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
1492 // a non-copy fixed-size array
1496 The first element was moved out of the array, but this is not
1497 possible because `NonCopy` does not implement the `Copy` trait.
1499 Consider borrowing the element instead of moving it:
1505 let array = [NonCopy; 1];
1506 let _value = &array[0]; // Borrowing is allowed, unlike moving.
1510 Alternatively, if your type implements `Clone` and you need to own the value,
1511 consider borrowing and then cloning:
1518 let array = [NonCopy; 1];
1519 // Now you can clone the array element.
1520 let _value = array[0].clone();
1526 This error occurs when an attempt is made to move out of a value whose type
1527 implements the `Drop` trait.
1529 Example of erroneous code:
1531 ```compile_fail,E0509
1540 impl Drop for DropStruct {
1541 fn drop(&mut self) {
1542 // Destruct DropStruct, possibly using FancyNum
1547 let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1548 let fancy_field = drop_struct.fancy; // Error E0509
1549 println!("Fancy: {}", fancy_field.num);
1550 // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1554 Here, we tried to move a field out of a struct of type `DropStruct` which
1555 implements the `Drop` trait. However, a struct cannot be dropped if one or
1556 more of its fields have been moved.
1558 Structs implementing the `Drop` trait have an implicit destructor that gets
1559 called when they go out of scope. This destructor may use the fields of the
1560 struct, so moving out of the struct could make it impossible to run the
1561 destructor. Therefore, we must think of all values whose type implements the
1562 `Drop` trait as single units whose fields cannot be moved.
1564 This error can be fixed by creating a reference to the fields of a struct,
1565 enum, or tuple using the `ref` keyword:
1576 impl Drop for DropStruct {
1577 fn drop(&mut self) {
1578 // Destruct DropStruct, possibly using FancyNum
1583 let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1584 let ref fancy_field = drop_struct.fancy; // No more errors!
1585 println!("Fancy: {}", fancy_field.num);
1586 // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1590 Note that this technique can also be used in the arms of a match expression:
1601 impl Drop for DropEnum {
1602 fn drop(&mut self) {
1603 // Destruct DropEnum, possibly using FancyNum
1608 // Creates and enum of type `DropEnum`, which implements `Drop`
1609 let drop_enum = DropEnum::Fancy(FancyNum{num: 10});
1611 // Creates a reference to the inside of `DropEnum::Fancy`
1612 DropEnum::Fancy(ref fancy_field) => // No error!
1613 println!("It was fancy-- {}!", fancy_field.num),
1615 // implicit call to `drop_enum.drop()` as drop_enum goes out of scope
1621 Closures cannot mutate immutable captured variables.
1623 Erroneous code example:
1625 ```compile_fail,E0595
1626 let x = 3; // error: closure cannot assign to immutable local variable `x`
1627 let mut c = || { x += 1 };
1630 Make the variable binding mutable:
1633 let mut x = 3; // ok!
1634 let mut c = || { x += 1 };
1639 This error occurs because you tried to mutably borrow a non-mutable variable.
1641 Example of erroneous code:
1643 ```compile_fail,E0596
1645 let y = &mut x; // error: cannot borrow mutably
1648 In here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it
1649 fails. To fix this error, you need to make `x` mutable:
1653 let y = &mut x; // ok!
1658 This error occurs because a borrow was made inside a variable which has a
1659 greater lifetime than the borrowed one.
1661 Example of erroneous code:
1663 ```compile_fail,E0597
1668 let mut x = Foo { x: None };
1670 x.x = Some(&y); // error: `y` does not live long enough
1673 In here, `x` is created before `y` and therefore has a greater lifetime. Always
1674 keep in mind that values in a scope are dropped in the opposite order they are
1675 created. So to fix the previous example, just make the `y` lifetime greater than
1684 let mut x = Foo { x: None };
1690 This error occurs because a borrow in a generator persists across a
1693 ```compile_fail,E0626
1694 # #![feature(generators, generator_trait)]
1695 # use std::ops::Generator;
1697 let a = &String::new(); // <-- This borrow...
1698 yield (); // ...is still in scope here, when the yield occurs.
1704 At present, it is not permitted to have a yield that occurs while a
1705 borrow is still in scope. To resolve this error, the borrow must
1706 either be "contained" to a smaller scope that does not overlap the
1707 yield or else eliminated in another way. So, for example, we might
1708 resolve the previous example by removing the borrow and just storing
1709 the integer by value:
1712 # #![feature(generators, generator_trait)]
1713 # use std::ops::Generator;
1722 This is a very simple case, of course. In more complex cases, we may
1723 wish to have more than one reference to the value that was borrowed --
1724 in those cases, something like the `Rc` or `Arc` types may be useful.
1726 This error also frequently arises with iteration:
1728 ```compile_fail,E0626
1729 # #![feature(generators, generator_trait)]
1730 # use std::ops::Generator;
1732 let v = vec![1,2,3];
1733 for &x in &v { // <-- borrow of `v` is still in scope...
1734 yield x; // ...when this yield occurs.
1740 Such cases can sometimes be resolved by iterating "by value" (or using
1741 `into_iter()`) to avoid borrowing:
1744 # #![feature(generators, generator_trait)]
1745 # use std::ops::Generator;
1747 let v = vec![1,2,3];
1748 for x in v { // <-- Take ownership of the values instead!
1749 yield x; // <-- Now yield is OK.
1755 If taking ownership is not an option, using indices can work too:
1758 # #![feature(generators, generator_trait)]
1759 # use std::ops::Generator;
1761 let v = vec![1,2,3];
1762 let len = v.len(); // (*)
1764 let x = v[i]; // (*)
1765 yield x; // <-- Now yield is OK.
1770 // (*) -- Unfortunately, these temporaries are currently required.
1771 // See <https://github.com/rust-lang/rust/issues/43122>.
1777 register_diagnostics! {
1778 // E0385, // {} in an aliasable location
1779 E0493, // destructors cannot be evaluated at compile-time
1780 E0524, // two closures require unique access to `..` at the same time
1781 E0526, // shuffle indices are not constant
1782 E0594, // cannot assign to {}
1783 E0598, // lifetime of {} is too short to guarantee its contents can be...
1784 E0625, // thread-local statics cannot be accessed at compile-time