@ -1683,7 +1683,7 @@ Instead of 50 clones, it's zero.
### Variables without values
### Variables without values
A variable without a value is called an "uninitialized" variable. Uninitialized means "hasn't started yet". They are simple, just `let` and the name:
A variable without a value is called an "uninitialized" variable. Uninitialized means "hasn't started yet". They are simple: just write `let` and the variable name:
```rust
```rust
fn main() {
fn main() {
@ -1691,11 +1691,11 @@ fn main() {
}
}
```
```
But you can't use it yet. Your program won't compile if it tries to use it.
But you can't use it yet, and Rust won't compile if anything is uninitialized.
But sometimes they can be useful. A good example is when:
But sometimes they can be useful. A good example is when:
- You have a code block and inside that is the value for your variable, and
- You have a code block and the value for your variable is inside it, and
- The variable needs to live outside of the code block.
- The variable needs to live outside of the code block.
It is important to know that `my_number` was declared in the `main()` function, so it lives until the end. But it gets its value from inside a loop. However, that value lives as long as `my_number`, because `my_number` has the value.
This prints `100`.
You can see that `my_number` was declared in the `main()` function, so it lives until the end. But it gets its value from inside a loop. However, that value lives as long as `my_number`, because `my_number` has the value. And if you wrote `let my_number = loop_then_return(number)` inside the block, it would just die right away.
It helps to imagine if you simplify the code. `loop_then_return(number)` gives the result 50, so let's delete it and write `50` instead. Also, now we don't need `number` so we will delete it too. Now it looks like this:
It helps to imagine if you simplify the code. `loop_then_return(number)` gives the result 100, so let's delete it and write `100` instead. Also, now we don't need `number` so we will delete it too. Now it looks like this:
```rust
```rust
fn main() {
fn main() {
let my_number;
let my_number;
{
{
my_number = 50;
my_number = 100;
}
}
println!("{}", my_number);
println!("{}", my_number);
}
}
```
```
So it's almost like saying `let my_number = { 50 };`.
So it's almost like saying `let my_number = { 100 };`.
Also note that `my_number` is not `mut`. We didn't give it a value until we gave it 50, so it never changed its value.
Also note that `my_number` is not `mut`. We didn't give it a value until we gave it 50, so it never changed its value. In the end, the real code for `my_number` is just let `my_number = 100;`.