Rust

E0381 Uninitialized Use Fix

The binding was declared, but no value is proven to reach the use - the assignment is missing, sits in only one branch, sits inside a loop that may never run, or covers only part of the value. This guide separates five shapes that produce E0381 and matches each one to the same repair in five positions: make an assignment happen on every path before the use, so the compiler can prove the binding is initialized.

Written November 24, 2026 · Tested with Rust 1.99.1 · 23 min read

By the end of this guide
  • The rule underneath: a use is allowed only where every path assigns first
  • How to read the diagnostic
  • The first failure: a plain use before any assignment
  • The second failure: an assignment in only one branch

Written November 24, 2026

Tested with Rust 1.99.1

What this guide is

Error E0381 is a use of a binding that has not been initialized, or that the compiler cannot prove has been initialized on the path that reaches the use. The error codes index states the rule in one line: it is not allowed to use or capture an uninitialized variable. Its example declares a binding with a type and no value, copies it into a second binding, and draws this error; its fix is to ensure that any declared variables are initialized before being used.

The five forms in this guide each reproduce E0381 with a small, complete program, and the missing initialization has a different shape in each one. A plain use with no assignment anywhere. An assignment inside only one branch of an if, so the other path reaches the use empty. An assignment inside a loop that may run zero times. A closure that captures the uninitialized binding, which the index's wording - use or capture - covers directly. And a partial assignment that sets one field of a struct, which the compiler rejects because partial initialization is not supported for an ordinary binding. In each family the failing program is small, the full compiler output is shown, and the fix makes an assignment cover every path, or initializes the whole value at the declaration.

Four neighboring shapes are not E0381 and are not in the families. A value used after it was moved is E0382. Moving a value out of a place behind a shared reference is E0507. Assigning twice to a binding that was never declared mutable is E0384, and a value dropped while a reference to it is still used is E0597. The traps section shows each one with its real code. One further program in the traps assigns the binding from an if/else expression in which both arms produce a value, and compiles - it is the boundary of this error, because there is no path on which the binding is empty.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: a use is allowed only where every path assigns first

A let statement can declare a binding with a type and no initializer, leaving the assignment to later code. That deferred form is legal - every failing program in this guide declares its binding the same way, and the fixed branch and loop programs keep the deferred declaration and assign later. What the compiler checks is not the declaration but each use: on every control-flow path that can reach the use, an assignment to the binding must come first. Where one path reaches the use without assigning, the binding on that path holds no value at all, and reading it would read whatever the storage happens to contain.

The compiler's wording separates the two cases. Where no assignment exists on any path, the header reads "isn't initialized". Where an assignment exists but some path skips it - one branch of an if, a loop body that may not execute - the header reads "is possibly-uninitialized", and the spans name the skipping path: the if condition that can be false, the loop that can run 0 times. The branch family's note adds the point readers most often argue with: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown. A condition the reader knows is always true does not rescue the program unless the compiler can see the same thing from the paths themselves.

Capturing counts as using. The index's rule covers a variable that is used or captured, and the closure family shows why: a closure that borrows its captures reads the captured binding when it is called, so creating the closure over an uninitialized binding is already the error, before any call happens. The diagnostic for that family marks the closure itself and adds a second marker, "borrow occurs due to use in closure", on the variable inside the body.

Partial assignment is a different refusal. Setting one field of a declared-but-uninitialized struct does not initialize the binding partway: the diagnostic calls it a partially assigned binding that isn't fully initialized, and its help states that partial initialization isn't supported - fully initialize the binding with a default value and mutate it, or use std::mem::MaybeUninit. MaybeUninit<T> is the standard library's wrapper for exactly the deferred case: its documentation describes it as a wrapper type to construct uninitialized instances of T, a signal to the compiler indicating that the data inside might not be initialized, with assume_init allowed only after the value has been properly initialized. It is the tool for low-level code that initializes memory piece by piece; for an ordinary binding whose fields are all known at one point, writing the full value at the declaration is the fix, and it is the fix the partial family uses.

How to read the diagnostic

The header names the binding and which of the two cases applies: "isn't initialized" when no path assigns, "is possibly-uninitialized" when an assignment exists on some paths only, and "partially assigned binding ... isn't fully initialized" when the program tried to initialize a value field by field. The first span marks the declaration with "binding declared here but left uninitialized" - the length of that marker shows whether mut was part of the declaration. The second span marks the use itself, with "used here but it isn't initialized" or "used here but it is possibly-uninitialized".

In the possibly-uninitialized families, read the middle spans before the use: they name the exact path that skips the assignment. The branch report underlines the if condition with "if this if condition is false, x is not initialized" and marks the closing brace with "an else arm might be missing here, initializing x". The loop report underlines the loop range with "if the for loop runs 0 times, x is not initialized". Those spans are the repair instructions: add the assignment to the path they name. Where a help block is present - "consider assigning a value", rewriting the declaration with an initializer - following it is the whole fix for the plain and closure families. The partial family carries no such rewrite; its help is a note naming the two real options, full initialization or MaybeUninit.

The first failure: a plain use before any assignment

The binding is declared with a type and no value, and the very next line reads it. There is no path question here at all - no assignment exists anywhere in the program:

Rust
fn main() {
    let x: i32;
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fail_plain.rs -o fail_plain
error[E0381]: used binding `x` isn't initialized
 --> fail_plain.rs:3:20
  |
2 |     let x: i32;
  |         - binding declared here but left uninitialized
3 |     println!("{}", x);
  |                    ^ `x` used here but it isn't initialized
  |
help: consider assigning a value
  |
2 |     let x: i32 = 42;
  |                ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0381`.

The header says "isn't initialized", not "possibly" - the compiler found no assignment to weigh. The help rewrites the declaration with an initializer, and following it exactly is the fix:

Rust
fn main() {
    let x: i32 = 42;
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fix_plain.rs -o fix_plain
$ ./fix_plain
42

The output is 42, the value the declaration now supplies. Deferred declaration earns its keep only when later code genuinely decides the value - the branch and loop families below - and even there, every path has to assign. When the value is already known at the declaration, writing it there removes the question entirely.

The second failure: an assignment in only one branch

The binding is assigned inside an if with no else. When the condition is false, control reaches the print with nothing assigned - and the compiler judges the paths, not the reader's knowledge of what the condition will be:

Rust
fn main() {
    let x: i32;
    let flag = std::env::args().count() > 1;
    if flag {
        x = 5;
    }
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fail_branch.rs -o fail_branch
error[E0381]: used binding `x` is possibly-uninitialized
 --> fail_branch.rs:7:20
  |
2 |     let x: i32;
  |         - binding declared here but left uninitialized
3 |     let flag = std::env::args().count() > 1;
4 |     if flag {
  |        ---- if this `if` condition is `false`, `x` is not initialized
5 |         x = 5;
6 |     }
  |      - an `else` arm might be missing here, initializing `x`
7 |     println!("{}", x);
  |                    ^ `x` used here but it is possibly-uninitialized
  |
  = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0381`.

The header has gained "possibly", the span on the condition names the false path, and the marker on the closing brace points at the missing else arm. Adding that arm, assigning on the path that previously skipped it, is the whole fix:

Rust
fn main() {
    let x: i32;
    let flag = std::env::args().count() > 1;
    if flag {
        x = 5;
    } else {
        x = 7;
    }
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fix_branch.rs -o fix_branch
$ ./fix_branch
7

The output is 7: run with no extra arguments, the condition is false, so the new else arm supplied the value - the very path that previously reached the print uninitialized. The deferred declaration survives here because both arms assign before the use; the binding is still assigned exactly once on any single run, so it needs no mut.

The third failure: an assignment inside a loop that may run zero times

A loop body is a path, and it is a path control may never take. The assignment sits inside a for over an empty range, so the path where the loop runs 0 times reaches the print with nothing assigned:

Rust
fn main() {
    let mut x: i32;
    for _ in 0..0 {
        x = 5;
    }
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fail_loop.rs -o fail_loop
error[E0381]: used binding `x` is possibly-uninitialized
 --> fail_loop.rs:6:20
  |
2 |     let mut x: i32;
  |         ----- binding declared here but left uninitialized
3 |     for _ in 0..0 {
  |              ---- if the `for` loop runs 0 times, `x` is not initialized
...
6 |     println!("{}", x);
  |                    ^ `x` used here but it is possibly-uninitialized

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0381`.

The span under the range states the skipping path outright: "if the for loop runs 0 times, x is not initialized". The compiler does not need the range to be empty in the source for this - any loop whose body might not execute leaves the same gap, including a while whose condition can be false on entry. The repair is to give the binding a value before the loop, so the use is covered whether or not the body ever runs:

Rust
fn main() {
    let mut x: i32 = 7;
    for _ in 0..3 {
        x = 5;
    }
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 fix_loop.rs -o fix_loop
$ ./fix_loop
5

The output is 5: the loop now runs three times and its assignment is the last one before the print. Had the range stayed empty, the print would have shown the pre-loop value 7 instead of reading an uninitialized binding - the initial value is both the fix and the fallback the loop was missing. The binding is mut in both versions, for two different reasons: in the failing version a loop-body assignment can run more than once, so it counts as reassignment rather than the one-time initialization a deferred immutable binding allows — the same program without mut draws E0384 alongside E0381 — and in the fixed version the loop assignment follows the pre-loop initializer, so it is a second assignment on the same path. A deferred binding assigned exactly once on each path, like the branch family's, needs no mut.

The fourth failure: a closure capturing an uninitialized binding

No line reads the binding directly. The closure's body reads it, and because a closure borrows what it captures, forming the closure over an uninitialized binding is itself the use the index forbids - the error fires at the closure, not at the call on the next line:

Rust
fn main() {
    let x: i32;
    let show = || println!("{}", x);
    show();
}
Terminal
$ rustc --edition 2021 fail_closure.rs -o fail_closure
error[E0381]: used binding `x` isn't initialized
 --> fail_closure.rs:3:16
  |
2 |     let x: i32;
  |         - binding declared here but left uninitialized
3 |     let show = || println!("{}", x);
  |                ^^                - borrow occurs due to use in closure
  |                |
  |                `x` used here but it isn't initialized
  |
help: consider assigning a value
  |
2 |     let x: i32 = 42;
  |                ++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0381`.

The first span marks the closure's parameter list, the second names the mechanism - "borrow occurs due to use in closure" - on the variable inside the body. Assigning the binding after the closure is created would be too late even if the call came later still: the capture happens where the closure is formed. The fix is the plain family's fix, applied before the closure exists:

Rust
fn main() {
    let x: i32 = 9;
    let show = || println!("{}", x);
    show();
}
Terminal
$ rustc --edition 2021 fix_closure.rs -o fix_closure
$ ./fix_closure
9

The output is 9, printed when show is called and reads its capture. The same shape with the assignment in only one branch, or inside a loop, before the closure would draw the possibly-uninitialized wording instead - the closure changes where the use is reported, not which paths have to assign.

The fifth failure: a partial assignment to one field

The program declares a struct binding and sets its first field, expecting to work through the value piece by piece. The compiler stops it at the field assignment itself: an ordinary binding cannot be initialized in parts, so the second field is never even reached:

Rust
struct Pair {
    a: i32,
    b: i32,
}

fn main() {
    let p: Pair;
    p.a = 1;
    println!("{}", p.b);
}
Terminal
$ rustc --edition 2021 fail_partial.rs -o fail_partial
error[E0381]: partially assigned binding `p` isn't fully initialized
 --> fail_partial.rs:8:5
  |
7 |     let p: Pair;
  |         - binding declared here but left uninitialized
8 |     p.a = 1;
  |     ^^^^^^^ `p` partially assigned here but it isn't fully initialized
  |
  = help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0381`.

The header is this family's own: "partially assigned binding p isn't fully initialized", and the span sits on the field assignment, not on the later print - the partial assignment is the error, before any use of the missing field. The help names the ordinary repair first: fully initialize the binding. A struct literal supplies every field at the declaration:

Rust
struct Pair {
    a: i32,
    b: i32,
}

fn main() {
    let p = Pair { a: 1, b: 2 };
    println!("{} {}", p.a, p.b);
}
Terminal
$ rustc --edition 2021 fix_partial.rs -o fix_partial
$ ./fix_partial
1 2

The output is 1 2, both fields, read from a value that was complete from its first line. Where the fields genuinely arrive at different times, the help's other routes apply: initialize the whole binding with a default value and mutate the fields afterward - which needs the binding declared mut - or hold the value in MaybeUninit while it is assembled and call assume_init only once every field is written. Reaching for either before checking whether all the values are in fact available at one point is wasted machinery: in most code that draws this error, they are.

The fixes in reach order

  • The header says "isn't initialized" and a help rewrites the declaration with an initializer: write that initializer - the value was known all along, or a closure is about to capture the binding.
  • The header says "is possibly-uninitialized" and a span names an if condition: add the else arm that assigns, so both paths assign before the use.
  • A span names a loop that can run 0 times: assign a value before the loop, and let the loop's assignment overwrite it when the body does run.
  • The header says "partially assigned binding": replace the field-by-field assignments with one full initialization at the declaration - a struct literal, or a default value followed by mutation - and reserve MaybeUninit for memory that is genuinely assembled piece by piece.
  • The report is E0382, E0507, E0384, or E0597 instead: the binding's initialization is not the problem - see the traps.

Traps

  • A value used after it was moved is E0382, not E0381. The binding here was fully initialized - and then given away, so the later use finds it empty for the opposite reason. The error codes index gives the cause: a variable was used after its contents have been moved elsewhere, because a type that is not Copy moves on assignment and a value cannot be owned by more than one variable. Cloning the value, or borrowing it instead of moving it, is the repair direction the index describes:
Rust
fn main() {
    let s = String::from("hello");
    let t = s;
    println!("{} {}", s, t);
}
Terminal
$ rustc --edition 2021 probe_moved.rs -o probe_moved
error[E0382]: borrow of moved value: `s`
 --> probe_moved.rs:4:23
  |
2 |     let s = String::from("hello");
  |         - move occurs because `s` has type `String`, which does not implement the `Copy` trait
3 |     let t = s;
  |             - value moved here
4 |     println!("{} {}", s, t);
  |                       ^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let t = s.clone();
  |              ++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
Trap
Read the header to tell the two apart: E0381's binding never received a value on the path in question; E0382's received one and moved it out. Initializing earlier fixes only the first. - Moving a value out of a place behind a shared reference is E0507. The pattern below tries to take ownership of a String out of an Option that the program only borrows, and the error codes index gives the cause: a borrowed value was moved out, because a borrow provides a reference to content owned elsewhere, not ownership of it. Its listed directions are to avoid moving the variable, to reclaim ownership, or to implement Copy on the type - and its help here points at the simplest one, matching on the reference instead of dereferencing it:
Rust
fn main() {
    let s = Some(String::from("hello"));
    let r = &s;
    if let Some(v) = *r {
        println!("{}", v);
    }
}
Terminal
$ rustc --edition 2021 probe_move_out.rs -o probe_move_out
error[E0507]: cannot move out of `r` as enum variant `Some` which is behind a shared reference
 --> probe_move_out.rs:4:22
  |
4 |     if let Some(v) = *r {
  |                 -    ^^
  |                 |
  |                 data moved here because `v` has type `String`, which does not implement the `Copy` trait
  |
help: consider removing the dereference here
  |
4 -     if let Some(v) = *r {
4 +     if let Some(v) = r {
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0507`.

Nothing in this program is uninitialized: every binding has a value. The missing piece is ownership, not initialization, and no initializer added anywhere repairs it. - Assigning a second time to a deferred binding that was never declared mutable is E0384. This trap sits closest to the families: the binding is declared without a value, assigned once - legally, that first assignment is its initialization - and then assigned again, which an immutable binding forbids. The error codes index is the entry the compiler names for it; the report also carries a warning that the first value is never read, because the second assignment overwrites it before any use:

Rust
fn main() {
    let x: i32;
    x = 5;
    x = 6;
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 probe_twice_assign.rs -o probe_twice_assign
error[E0384]: cannot assign twice to immutable variable `x`
 --> probe_twice_assign.rs:4:5
  |
3 |     x = 5;
  |     ----- first assignment to `x`
4 |     x = 6;
  |     ^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut x: i32;
  |         +++

warning: value assigned to `x` is never read
 --> probe_twice_assign.rs:3:5
  |
3 |     x = 5;
  |     ^^^^^ this value is reassigned later and never used
4 |     x = 6;
  |     ----- `x` is overwritten here before the previous value is read
  |
  = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default

error: aborting due to 1 previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0384`.

The deferred form allows exactly one assignment per path. A binding that is assigned in two branches needs no mut - each run assigns it once - but a binding assigned twice in sequence on the same path does, and the help's mut is the whole repair. - A value dropped while it is still borrowed is E0597, not E0381 - and the value here is initialized, which is the tell. A reference is stored out of the block that owns its value, and the error codes index gives the cause: a value was dropped while it was still borrowed. The repair is to keep the value alive at least as long as the reference is used, or to stop using the reference after the block:

Rust
fn main() {
    let r;
    {
        let x = 5;
        r = &x;
    }
    println!("{}", r);
}
Terminal
$ rustc --edition 2021 probe_scope.rs -o probe_scope
error[E0597]: `x` does not live long enough
 --> probe_scope.rs:5:13
  |
4 |         let x = 5;
  |             - binding `x` declared here
5 |         r = &x;
  |             ^^ borrowed value does not live long enough
6 |     }
  |     - `x` dropped here while still borrowed
7 |     println!("{}", r);
  |                    - borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0597`.

The outer binding r is itself a deferred declaration, which can make this report look like an initialization problem at a glance. It is not: r is assigned on the only path, and the error is about the lifetime of the value it points at. - A binding assigned from an if/else expression compiles, with no deferred declaration at all. Both arms produce a value, the expression's value is whichever arm ran, and the binding is initialized by construction - there is no path on which it could be empty. This is the boundary of E0381, and often the cleanest form of the branch family's fix when the two arms differ only in the value they supply:

Rust
fn main() {
    let flag = std::env::args().count() > 1;
    let x: i32 = if flag { 5 } else { 7 };
    println!("{}", x);
}
Terminal
$ rustc --edition 2021 boundary_expr.rs -o boundary_expr
$ ./boundary_expr
7

The output is 7, from the else arm, run with no extra arguments. Reach for this form when both branches compute a value for the same binding; reach for the branch family's two assigning arms when each branch also does other work.

Checklist

  • The header read for which case applies: "isn't initialized", "is possibly-uninitialized", or "partially assigned binding ... isn't fully initialized".
  • The spans followed to the path that skips the assignment: the if condition that can be false, the missing else arm, the loop that can run 0 times.
  • An assignment added to every path the spans name - not just the path the program usually takes.
  • A closure over the binding checked at its creation line, where the capture happens, not at the call.
  • A partially assigned value replaced by one full initialization, or held in MaybeUninit only where the value is genuinely assembled piece by piece.
  • A binding assigned twice in sequence given mut, and a binding assigned once per branch left immutable.
  • A report of E0382, E0507, E0384, or E0597 repaired by its own rule - clone or borrow instead of moving, match on the reference, declare mut, or keep the value alive - not by adding an initializer.
  • The fixed program run, and its output checked against what the failing program was trying to compute.

Sources

  • Rust, Error codes index: E0381 - it is not allowed to use or capture an uninitialized variable; in its example a binding declared with a type and no value is copied into a second binding and draws the error, and the fix is to ensure that any declared variables are initialized before being used, as in its corrected example, which initializes the binding at the declaration.
  • Rust, std::mem::MaybeUninit documentation - a wrapper type to construct uninitialized instances of T; it is a signal to the compiler indicating that the data inside might not be initialized, which is why creating one is not itself undefined behavior, and calling assume_init is allowed only after the value has been properly initialized; the documentation also gives field-by-field struct initialization and element-by-element array initialization as uses.
  • The Rust Reference, Statements - a let statement introduces a new set of variables given by a pattern, followed optionally by a type annotation and then either ending, or followed by an initializer expression; variables introduced by a declaration are visible from the point of declaration until the end of the enclosing block scope.
  • Rust, Error codes index: E0382 - a variable was used after its contents have been moved elsewhere; a type not marked Copy moves on assignment because a value cannot be owned by more than one variable, and borrowing the value with a reference, or duplicating it with clone() where the type implements Clone, are the directions the entry describes.
  • Rust, Error codes index: E0507 - a borrowed value was moved out; a method or pattern that takes ownership cannot be satisfied from a borrow, which provides only a reference to content owned elsewhere, and the entry's directions are to avoid moving the variable, to reclaim ownership, or to implement the Copy trait on the type.
  • Rust, Error codes index: E0384 - an immutable variable was reassigned; by default variables in Rust are immutable, and the entry's fix is to add mut after let when declaring the variable, or to initialize a new variable instead, including by shadowing.
  • Rust, Error codes index: E0597 - a value was dropped while it was still borrowed; in its example a value assigned into a longer-lived holder inside a block is dropped at the end of that block while the holder still borrows it, and removing the scope so the value is not dropped until after the last use is the fix.

Keep Exploring