Rewrite variables without values

pull/67/head
Dhghomon 4 years ago committed by GitHub
parent e97165471e
commit 6b3382a81d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1683,7 +1683,7 @@ Instead of 50 clones, it's zero.
### 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
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:
- 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.
```rust
@ -1712,11 +1712,12 @@ fn loop_then_return(mut counter: i32) -> i32 {
fn main() {
let my_number;
{
// Pretend we need to have this code block
{ // Pretend we need to have this code block
let number = {
// Pretend there is code here to make a number
7
// Lots of code, and finally:
57
};
my_number = loop_then_return(number);
@ -1726,24 +1727,26 @@ fn main() {
}
```
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
fn main() {
let my_number;
{
my_number = 50;
my_number = 100;
}
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;`.
## Collection types

Loading…
Cancel
Save