Skip to content

Commit fdd9a32

Browse files
Merge pull request #366 from Havvy/empty_statements
Improve verbiage around let statements and variable initialization
2 parents 33dfdbd + edab6e4 commit fdd9a32

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/statements.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ fn outer() {
4242

4343
### `let` statements
4444

45-
A *`let` statement* introduces a new set of variables, given by a pattern. The
46-
pattern may be followed by a type annotation, and/or an initializer expression.
47-
When no type annotation is given, the compiler will infer the type, or signal
48-
an error if insufficient type information is available for definite inference.
49-
Any variables introduced by a variable declaration are visible from the point of
50-
declaration until the end of the enclosing block scope.
45+
A *`let` statement* introduces a new set of [variables], given by a pattern. The
46+
pattern is followed optionally by a type annotation and then optionally by an
47+
initializer expression. When no type annotation is given, the compiler will
48+
infer the type, or signal an error if insufficient type information is
49+
available for definite inference. Any variables introduced by a variable
50+
declaration are visible from the point of declaration until the end of the
51+
enclosing block scope.
5152

5253
## Expression statements
5354

@@ -96,3 +97,4 @@ if true {
9697
[module]: items/modules.html
9798
[canonical path]: paths.html#canonical-paths
9899
[implementations]: items/implementations.html
100+
[variables]: variables.html

src/variables.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ variable.
77
A _local variable_ (or *stack-local* allocation) holds a value directly,
88
allocated within the stack's memory. The value is a part of the stack frame.
99

10-
Local variables are immutable unless declared otherwise. For example: `let mut x = ...`.
10+
Local variables are immutable unless declared otherwise. For example:
11+
`let mut x = ...`.
1112

1213
Function parameters are immutable unless declared with `mut`. The `mut` keyword
13-
applies only to the following parameter. For example: `|mut x, y|` and `fn f(mut x:
14-
Box<i32>, y: Box<i32>)` declare one mutable variable `x` and one immutable
15-
variable `y`.
14+
applies 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
16+
immutable variable `y`.
1617

1718
Methods that take either `self` or `Box<Self>` can optionally place them in a
18-
mutable variable by prefixing them with `mut` (similar to regular arguments). For example:
19+
mutable variable by prefixing them with `mut` (similar to regular arguments).
20+
For example:
1921

2022
```rust
2123
trait Changer: Sized {
@@ -24,8 +26,29 @@ trait Changer: Sized {
2426
}
2527
```
2628

27-
Local variables are not initialized when allocated. Instead, the entire frame worth of
28-
local variables are allocated, on frame-entry, in an uninitialized
29+
Local variables are not initialized when allocated. Instead, the entire frame
30+
worth of local variables are allocated, on frame-entry, in an uninitialized
2931
state. Subsequent statements within a function may or may not initialize the
3032
local variables. Local variables can be used only after they have been
31-
initialized; this is enforced by the compiler.
33+
initialized through all reachable control flow paths.
34+
35+
In this next example, `init_after_if` is initialized after the [`if` expression]
36+
while `uninit_after_if` is not because it is not initialized in the `else` case.
37+
38+
```rust
39+
# fn random_bool() -> bool { true }
40+
fn initialization_example() {
41+
let init_after_if: ();
42+
let uninit_after_if: ();
43+
44+
if random_bool() {
45+
init_after_if = ();
46+
uninit_after_if = ();
47+
} else {
48+
init_after_if = ();
49+
}
50+
51+
init_after_if; // ok
52+
// uninit_after_if; // err: use of possibly uninitialized `uninit_after_if`
53+
}
54+
```

0 commit comments

Comments
 (0)