src/doc/rustc-dev-guide/src/borrow-check/moves-and-initialization.md MARKDOWN 51 lines View on github.com → Search inside
1# Tracking moves and initialization23Part of the borrow checker's job is to track which variables are4"initialized" at any given point in time -- this also requires5figuring out where moves occur and tracking those.67## Initialization and moves89From a user's perspective, initialization -- giving a variable some10value -- and moves -- transferring ownership to another place -- might11seem like distinct topics. Indeed, our borrow checker error messages12often talk about them differently. But **within the borrow checker**,13they are not nearly as separate. Roughly speaking, the borrow checker14tracks the set of "initialized places" at any point in the source15code. Assigning to a previously uninitialized local variable adds it16to that set; moving from a local variable removes it from that set.1718Consider this example:1920```rust,ignore21fn foo() {22    let a: Vec<u32>;2324    // a is not initialized yet2526    a = vec![22];2728    // a is initialized here2930    std::mem::drop(a); // a is moved here3132    // a is no longer initialized here3334    let l = a.len(); //~ ERROR35}36```3738Here you can see that `a` starts off as uninitialized; once it is39assigned, it becomes initialized. But when `drop(a)` is called, that40moves `a` into the call, and hence it becomes uninitialized again.4142## Subsections4344To make it easier to peruse, this section is broken into a number of45subsections:4647- [Move paths](./moves-and-initialization/move-paths.md) the48  *move path* concept that we use to track which local variables (or parts of49  local variables, in some cases) are initialized.50- TODO *Rest not yet written* =)

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.