1# Variables
2
3A _variable_ is a component of a stack frame, either a named function parameter,
4an anonymous [temporary](expressions.md#temporaries), or a named local
5variable.
6
7A _local variable_ (or *stack-local* allocation) holds a value directly,
8allocated within the stack's memory. The value is a part of the stack frame.
9
10Local variables are immutable unless declared otherwise. For example:
11`let mut x = ...`.
12
13Function parameters are immutable unless declared with `mut`. The `mut` keyword
14applies only to the following parameter. For example: `|mut x, y|` and
15`fn f(mut x: Box<i32>, y: Box<i32>)` declare one mutable variable `x` and one
16immutable variable `y`.
17
18Local variables are not initialized when allocated. Instead, the entire frame
19worth of local variables are allocated, on frame-entry, in an uninitialized
20state. Subsequent statements within a function may or may not initialize the
21local variables. Local variables can be used only after they have been
22initialized through all reachable control flow paths.
23
24In this next example, `init_after_if` is initialized after the [`if` expression]
25while `uninit_after_if` is not because it is not initialized in the `else` case.
26
27```rust
28# fn random_bool() -> bool { true }
29fn initialization_example() {
30    let init_after_if: ();
31    let uninit_after_if: ();
32
33    if random_bool() {
34        init_after_if = ();
35        uninit_after_if = ();
36    } else {
37        init_after_if = ();
38    }
39
40    init_after_if; // ok
41    // uninit_after_if; // err: use of possibly uninitialized `uninit_after_if`
42}
43```
44
45[`if` expression]: expressions/if-expr.md#if-expressions
46