about safety.
"##,
-E0381: r##"
-It is not allowed to use or capture an uninitialized variable. For example:
-
-```compile_fail,E0381
-fn main() {
- let x: i32;
- let y = x; // error, use of possibly uninitialized variable
-}
-```
-
-To fix this, ensure that any declared variables are initialized before being
-used. Example:
-
-```
-fn main() {
- let x: i32 = 0;
- let y = x; // ok!
-}
-```
-"##,
-
E0382: r##"
This error occurs when an attempt is made to use a variable after its contents
have been moved elsewhere. For example:
```
"##,
-E0384: r##"
-This error occurs when an attempt is made to reassign an immutable variable.
-For example:
-
-```compile_fail,E0384
-fn main() {
- let x = 3;
- x = 5; // error, reassignment of immutable variable
-}
-```
-
-By default, variables in Rust are immutable. To fix this error, add the keyword
-`mut` after the keyword `let` when declaring the variable. For example:
-
-```
-fn main() {
- let mut x = 3;
- x = 5;
-}
-```
-"##,
-
/*E0386: r##"
This error occurs when an attempt is made to mutate the target of a mutable
reference stored inside an immutable container.
```
"##,
-E0499: r##"
-A variable was borrowed as mutable more than once. Erroneous code example:
-
-```compile_fail,E0499
-let mut i = 0;
-let mut x = &mut i;
-let mut a = &mut i;
-// error: cannot borrow `i` as mutable more than once at a time
-```
-
-Please note that in rust, you can either have many immutable references, or one
-mutable reference. Take a look at
-https://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
-for more information. Example:
-
-
-```
-let mut i = 0;
-let mut x = &mut i; // ok!
-
-// or:
-let mut i = 0;
-let a = &i; // ok!
-let b = &i; // still ok!
-let c = &i; // still ok!
-```
-"##,
-
-E0500: r##"
-A borrowed variable was used in another closure. Example of erroneous code:
-
-```compile_fail
-fn you_know_nothing(jon_snow: &mut i32) {
- let nights_watch = || {
- *jon_snow = 2;
- };
- let starks = || {
- *jon_snow = 3; // error: closure requires unique access to `jon_snow`
- // but it is already borrowed
- };
-}
-```
-
-In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
-cannot be borrowed by the `starks` closure at the same time. To fix this issue,
-you can put the closure in its own scope:
-
-```
-fn you_know_nothing(jon_snow: &mut i32) {
- {
- let nights_watch = || {
- *jon_snow = 2;
- };
- } // At this point, `jon_snow` is free.
- let starks = || {
- *jon_snow = 3;
- };
-}
-```
-
-Or, if the type implements the `Clone` trait, you can clone it between
-closures:
-
-```
-fn you_know_nothing(jon_snow: &mut i32) {
- let mut jon_copy = jon_snow.clone();
- let nights_watch = || {
- jon_copy = 2;
- };
- let starks = || {
- *jon_snow = 3;
- };
-}
-```
-"##,
-
-E0501: r##"
-This error indicates that a mutable variable is being used while it is still
-captured by a closure. Because the closure has borrowed the variable, it is not
-available for use until the closure goes out of scope.
-
-Note that a capture will either move or borrow a variable, but in this
-situation, the closure is borrowing the variable. Take a look at
-http://rustbyexample.com/fn/closures/capture.html for more information about
-capturing.
-
-Example of erroneous code:
-
-```compile_fail,E0501
-fn inside_closure(x: &mut i32) {
- // Actions which require unique access
-}
-
-fn outside_closure(x: &mut i32) {
- // Actions which require unique access
-}
-
-fn foo(a: &mut i32) {
- let bar = || {
- inside_closure(a)
- };
- outside_closure(a); // error: cannot borrow `*a` as mutable because previous
- // closure requires unique access.
-}
-```
-
-To fix this error, you can place the closure in its own scope:
-
-```
-fn inside_closure(x: &mut i32) {}
-fn outside_closure(x: &mut i32) {}
-
-fn foo(a: &mut i32) {
- {
- let bar = || {
- inside_closure(a)
- };
- } // borrow on `a` ends.
- outside_closure(a); // ok!
-}
-```
-
-Or you can pass the variable as a parameter to the closure:
-
-```
-fn inside_closure(x: &mut i32) {}
-fn outside_closure(x: &mut i32) {}
-
-fn foo(a: &mut i32) {
- let bar = |s: &mut i32| {
- inside_closure(s)
- };
- outside_closure(a);
- bar(a);
-}
-```
-
-It may be possible to define the closure later:
-
-```
-fn inside_closure(x: &mut i32) {}
-fn outside_closure(x: &mut i32) {}
-
-fn foo(a: &mut i32) {
- outside_closure(a);
- let bar = || {
- inside_closure(a)
- };
-}
-```
-"##,
-
-E0502: r##"
-This error indicates that you are trying to borrow a variable as mutable when it
-has already been borrowed as immutable.
-
-Example of erroneous code:
-
-```compile_fail,E0502
-fn bar(x: &mut i32) {}
-fn foo(a: &mut i32) {
- let ref y = a; // a is borrowed as immutable.
- bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
- // as immutable
-}
-```
-
-To fix this error, ensure that you don't have any other references to the
-variable before trying to access it mutably:
-
-```
-fn bar(x: &mut i32) {}
-fn foo(a: &mut i32) {
- bar(a);
- let ref y = a; // ok!
-}
-```
-
-For more information on the rust ownership system, take a look at
-https://doc.rust-lang.org/book/first-edition/references-and-borrowing.html.
-"##,
-
-E0503: r##"
-A value was used after it was mutably borrowed.
-
-Example of erroneous code:
-
-```compile_fail,E0503
-fn main() {
- let mut value = 3;
- // Create a mutable borrow of `value`. This borrow
- // lives until the end of this function.
- let _borrow = &mut value;
- let _sum = value + 1; // error: cannot use `value` because
- // it was mutably borrowed
-}
-```
-
-In this example, `value` is mutably borrowed by `borrow` and cannot be
-used to calculate `sum`. This is not possible because this would violate
-Rust's mutability rules.
-
-You can fix this error by limiting the scope of the borrow:
-
-```
-fn main() {
- let mut value = 3;
- // By creating a new block, you can limit the scope
- // of the reference.
- {
- let _borrow = &mut value; // Use `_borrow` inside this block.
- }
- // The block has ended and with it the borrow.
- // You can now use `value` again.
- let _sum = value + 1;
-}
-```
-
-Or by cloning `value` before borrowing it:
-
-```
-fn main() {
- let mut value = 3;
- // We clone `value`, creating a copy.
- let value_cloned = value.clone();
- // The mutable borrow is a reference to `value` and
- // not to `value_cloned`...
- let _borrow = &mut value;
- // ... which means we can still use `value_cloned`,
- let _sum = value_cloned + 1;
- // even though the borrow only ends here.
-}
-```
-
-You can find more information about borrowing in the rust-book:
-http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
-"##,
-
-E0504: r##"
-This error occurs when an attempt is made to move a borrowed variable into a
-closure.
-
-Example of erroneous code:
-
-```compile_fail,E0504
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let fancy_num = FancyNum { num: 5 };
- let fancy_ref = &fancy_num;
-
- let x = move || {
- println!("child function: {}", fancy_num.num);
- // error: cannot move `fancy_num` into closure because it is borrowed
- };
-
- x();
- println!("main function: {}", fancy_ref.num);
-}
-```
-
-Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into
-the closure `x`. There is no way to move a value into a closure while it is
-borrowed, as that would invalidate the borrow.
-
-If the closure can't outlive the value being moved, try using a reference
-rather than moving:
-
-```
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let fancy_num = FancyNum { num: 5 };
- let fancy_ref = &fancy_num;
-
- let x = move || {
- // fancy_ref is usable here because it doesn't move `fancy_num`
- println!("child function: {}", fancy_ref.num);
- };
-
- x();
-
- println!("main function: {}", fancy_num.num);
-}
-```
-
-If the value has to be borrowed and then moved, try limiting the lifetime of
-the borrow using a scoped block:
-
-```
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let fancy_num = FancyNum { num: 5 };
-
- {
- let fancy_ref = &fancy_num;
- println!("main function: {}", fancy_ref.num);
- // `fancy_ref` goes out of scope here
- }
-
- let x = move || {
- // `fancy_num` can be moved now (no more references exist)
- println!("child function: {}", fancy_num.num);
- };
-
- x();
-}
-```
-
-If the lifetime of a reference isn't enough, such as in the case of threading,
-consider using an `Arc` to create a reference-counted value:
-
-```
-use std::sync::Arc;
-use std::thread;
-
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let fancy_ref1 = Arc::new(FancyNum { num: 5 });
- let fancy_ref2 = fancy_ref1.clone();
-
- let x = thread::spawn(move || {
- // `fancy_ref1` can be moved and has a `'static` lifetime
- println!("child thread: {}", fancy_ref1.num);
- });
-
- x.join().expect("child thread should finish");
- println!("main thread: {}", fancy_ref2.num);
-}
-```
-"##,
-
-E0505: r##"
-A value was moved out while it was still borrowed.
-
-Erroneous code example:
-
-```compile_fail,E0505
-struct Value {}
-
-fn eat(val: Value) {}
-
-fn main() {
- let x = Value{};
- {
- let _ref_to_val: &Value = &x;
- eat(x);
- }
-}
-```
-
-Here, the function `eat` takes the ownership of `x`. However,
-`x` cannot be moved because it was borrowed to `_ref_to_val`.
-To fix that you can do few different things:
-
-* Try to avoid moving the variable.
-* Release borrow before move.
-* Implement the `Copy` trait on the type.
-
-Examples:
-
-```
-struct Value {}
-
-fn eat(val: &Value) {}
-
-fn main() {
- let x = Value{};
- {
- let _ref_to_val: &Value = &x;
- eat(&x); // pass by reference, if it's possible
- }
-}
-```
-
-Or:
-
-```
-struct Value {}
-
-fn eat(val: Value) {}
-
-fn main() {
- let x = Value{};
- {
- let _ref_to_val: &Value = &x;
- }
- eat(x); // release borrow and then move it.
-}
-```
-
-Or:
-
-```
-#[derive(Clone, Copy)] // implement Copy trait
-struct Value {}
-
-fn eat(val: Value) {}
-
-fn main() {
- let x = Value{};
- {
- let _ref_to_val: &Value = &x;
- eat(x); // it will be copied here.
- }
-}
-```
-
-You can find more information about borrowing in the rust-book:
-http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
-"##,
-
-E0506: r##"
-This error occurs when an attempt is made to assign to a borrowed value.
-
-Example of erroneous code:
-
-```compile_fail,E0506
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let mut fancy_num = FancyNum { num: 5 };
- let fancy_ref = &fancy_num;
- fancy_num = FancyNum { num: 6 };
- // error: cannot assign to `fancy_num` because it is borrowed
-
- println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
-}
-```
-
-Because `fancy_ref` still holds a reference to `fancy_num`, `fancy_num` can't
-be assigned to a new value as it would invalidate the reference.
-
-Alternatively, we can move out of `fancy_num` into a second `fancy_num`:
-
-```
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let mut fancy_num = FancyNum { num: 5 };
- let moved_num = fancy_num;
- fancy_num = FancyNum { num: 6 };
-
- println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
-}
-```
-
-If the value has to be borrowed, try limiting the lifetime of the borrow using
-a scoped block:
-
-```
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let mut fancy_num = FancyNum { num: 5 };
-
- {
- let fancy_ref = &fancy_num;
- println!("Ref: {}", fancy_ref.num);
- }
-
- // Works because `fancy_ref` is no longer in scope
- fancy_num = FancyNum { num: 6 };
- println!("Num: {}", fancy_num.num);
-}
-```
-
-Or by moving the reference into a function:
-
-```
-struct FancyNum {
- num: u8,
-}
-
-fn main() {
- let mut fancy_num = FancyNum { num: 5 };
-
- print_fancy_ref(&fancy_num);
-
- // Works because function borrow has ended
- fancy_num = FancyNum { num: 6 };
- println!("Num: {}", fancy_num.num);
-}
-
-fn print_fancy_ref(fancy_ref: &FancyNum){
- println!("Ref: {}", fancy_ref.num);
-}
-```
-"##,
-
E0507: r##"
You tried to move out of a value which was borrowed. Erroneous code example:
register_diagnostics! {
// E0385, // {} in an aliasable location
- E0524, // two closures require unique access to `..` at the same time
E0594, // cannot assign to {}
E0598, // lifetime of {} is too short to guarantee its contents can be...
}