Rewrite up to shadowing

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

@ -719,7 +719,7 @@ fn main() {
The compiler says: `error[E0384]: cannot assign twice to immutable variable my_number`. This is because variables are immutable if you only write `let`.
To change a variable, add `mut`:
But sometimes you want to change your variable. To make a variable that you can change, add `mut` after `let`:
```rust
fn main() {
@ -730,11 +730,11 @@ fn main() {
Now there is no problem.
However, you cannot change the type even with `mut`. This will not work:
However, you cannot change the type: even `mut` doesn't let you do that. This will not work:
```rust
fn main() {
let mut my_variable = 8;
let mut my_variable = 8; // it is now an i32. That can't be changed
my_variable = "Hello, world!"; // ⚠️
}
```
@ -749,19 +749,17 @@ Shadowing means using `let` to declare a new variable with the same name as anot
fn main() {
let my_number = 8; // This is an i32
println!("{}", my_number); // prints 8
let my_number = 9.2; // This is an f64. It is not my_number - it is completely different!
let my_number = 9.2; // This is an f64 with the same name. But it's not the first my_number - it is completely different!
println!("{}", my_number) // Prints 9.2
}
```
Here we say that we "shadowed" `my_number` with a new "let binding".
So is the first `my_number` destroyed? No, but when we call `my_number` we now get `my_number` the `f64`. And because they are in the same scope block, we can't see the first `my_number` anymore.
So is the first `my_number` destroyed? No, but when we call `my_number` we now get `my_number` the `f64`. And because they are in the same scope block (the same `{}`), we can't see the first `my_number` anymore.
But if they are in different blocks, we can see both. For example:
So when you shadow a variable, you don't destroy it. You **block** it.
```rust
fn main() {
let my_number = 8; // This is an i32
@ -776,9 +774,15 @@ fn main() {
}
```
So what is the advantage of shadowing? Shadowing is good when you need to change a variable a lot.
So when you shadow a variable, you don't destroy it. You **block** it.
So what is the advantage of shadowing? Shadowing is good when you need to change a variable a lot. Imagine that you want to do a lot of simple math with a variable:
```rust
fn times_two(number: i32) -> i32 {
number * 2
}
fn main() {
let final_number = {
let y = 10;
@ -789,32 +793,30 @@ fn main() {
};
println!("The number is now: {}", final_number)
}
fn times_two(number: i32) -> i32 {
number * 2
}
```
Without shadowing you would have to think of different names, even though you don't care about x:
```rust
fn times_two(number: i32) -> i32 {
number * 2
}
fn main() {
// Pretending we are using Rust without shadowing
let final_number = {
let y = 10;
let x = 9; // x starts at 9
let x_twice = times_two(x); // second name for x
let x_twice_and_y = x_twice + y; // third name for x
let x_twice_and_y = x_twice + y; // third name for x!
x_twice_and_y // too bad we didn't have shadowing - we could have just used x
};
println!("The number is now: {}", final_number)
}
fn times_two(number: i32) -> i32 {
number * 2
}
```
In general, you see shadowing in Rust in this case. It happens where you want to quickly take variable, do something to it, and do something else again. And you usually use it for quick variables that you don't care too much about.
## The stack, the heap, and pointers
The stack, the heap, and pointers are very important in Rust.

Loading…
Cancel
Save