From 3e78e5fb1e0b87f999f9b301a3c6badc833f1b65 Mon Sep 17 00:00:00 2001 From: Manichand Kondapaka Date: Fri, 16 Feb 2024 17:16:29 +0000 Subject: [PATCH 1/4] Added blocks and scopes section --- src/SUMMARY.md | 1 + src/control-flow-basics/blocks-and-scopes.md | 31 ---------------- .../blocks-and-scopes/scopes,md | 35 +++++++++++++++++++ 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/control-flow-basics/blocks-and-scopes/scopes,md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9f64e43389d..12648b5265a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -37,6 +37,7 @@ - [`break` and `continue`](control-flow-basics/break-continue.md) - [Labels](control-flow-basics/break-continue/labels.md) - [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md) + - [Scopes](control-flow-basics/blocks-and-scopes/scopes.md) - [Functions](control-flow-basics/functions.md) - [Macros](control-flow-basics/macros.md) - [Exercise: Collatz Sequence](control-flow-basics/exercise.md) diff --git a/src/control-flow-basics/blocks-and-scopes.md b/src/control-flow-basics/blocks-and-scopes.md index 76a1da0c999..934005249e8 100644 --- a/src/control-flow-basics/blocks-and-scopes.md +++ b/src/control-flow-basics/blocks-and-scopes.md @@ -24,40 +24,9 @@ fn main() { If the last expression ends with `;`, then the resulting value and type is `()`. -## Scopes and Shadowing - -A variable's scope is limited to the enclosing block. - -You can shadow variables, both those from outer scopes and variables from the -same scope: - -```rust,editable -fn main() { - let a = 10; - println!("before: {a}"); - { - let a = "hello"; - println!("inner scope: {a}"); - - let a = true; - println!("shadowed in inner scope: {a}"); - } - - println!("after: {a}"); -} -``` -
- You can show how the value of the block changes by changing the last line in the block. For instance, adding/removing a semicolon or using a `return`. -- Show that a variable's scope is limited by adding a `b` in the inner block in - the last example, and then trying to access it outside that block. -- Shadowing is different from mutation, because after shadowing both variable's - memory locations exist at the same time. Both are available under the same - name, depending where you use it in the code. -- A shadowing variable can have a different type. -- Shadowing looks obscure at first, but is convenient for holding on to values - after `.unwrap()`.
diff --git a/src/control-flow-basics/blocks-and-scopes/scopes,md b/src/control-flow-basics/blocks-and-scopes/scopes,md new file mode 100644 index 00000000000..af293899ad9 --- /dev/null +++ b/src/control-flow-basics/blocks-and-scopes/scopes,md @@ -0,0 +1,35 @@ +## Scopes and Shadowing + +A variable's scope is limited to the enclosing block. + +You can shadow variables, both those from outer scopes and variables from the +same scope: + +```rust,editable +fn main() { + let a = 10; + println!("before: {a}"); + { + let a = "hello"; + println!("inner scope: {a}"); + + let a = true; + println!("shadowed in inner scope: {a}"); + } + + println!("after: {a}"); +} +``` + +
+ +- Show that a variable's scope is limited by adding a `b` in the inner block in + the last example, and then trying to access it outside that block. +- Shadowing is different from mutation, because after shadowing both variable's + memory locations exist at the same time. Both are available under the same + name, depending where you use it in the code. +- A shadowing variable can have a different type. +- Shadowing looks obscure at first, but is convenient for holding on to values + after `.unwrap()`. + +
From 5d7070a1f8d5c7ff8edd88872257ed0342ce0334 Mon Sep 17 00:00:00 2001 From: Manichand Date: Fri, 16 Feb 2024 22:54:44 +0530 Subject: [PATCH 2/4] file name updated --- .../blocks-and-scopes/scopes,md | 35 ------------------- .../blocks-and-scopes/scopes.md | 1 + 2 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 src/control-flow-basics/blocks-and-scopes/scopes,md create mode 100644 src/control-flow-basics/blocks-and-scopes/scopes.md diff --git a/src/control-flow-basics/blocks-and-scopes/scopes,md b/src/control-flow-basics/blocks-and-scopes/scopes,md deleted file mode 100644 index af293899ad9..00000000000 --- a/src/control-flow-basics/blocks-and-scopes/scopes,md +++ /dev/null @@ -1,35 +0,0 @@ -## Scopes and Shadowing - -A variable's scope is limited to the enclosing block. - -You can shadow variables, both those from outer scopes and variables from the -same scope: - -```rust,editable -fn main() { - let a = 10; - println!("before: {a}"); - { - let a = "hello"; - println!("inner scope: {a}"); - - let a = true; - println!("shadowed in inner scope: {a}"); - } - - println!("after: {a}"); -} -``` - -
- -- Show that a variable's scope is limited by adding a `b` in the inner block in - the last example, and then trying to access it outside that block. -- Shadowing is different from mutation, because after shadowing both variable's - memory locations exist at the same time. Both are available under the same - name, depending where you use it in the code. -- A shadowing variable can have a different type. -- Shadowing looks obscure at first, but is convenient for holding on to values - after `.unwrap()`. - -
diff --git a/src/control-flow-basics/blocks-and-scopes/scopes.md b/src/control-flow-basics/blocks-and-scopes/scopes.md new file mode 100644 index 00000000000..25cd4513bf2 --- /dev/null +++ b/src/control-flow-basics/blocks-and-scopes/scopes.md @@ -0,0 +1 @@ +# Scopes From 6ad7693adcaa19967cb8fb7a06e7822116e09ed6 Mon Sep 17 00:00:00 2001 From: Manichand Date: Fri, 16 Feb 2024 22:58:01 +0530 Subject: [PATCH 3/4] Added scopes --- .../blocks-and-scopes/scopes.md | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/control-flow-basics/blocks-and-scopes/scopes.md b/src/control-flow-basics/blocks-and-scopes/scopes.md index 25cd4513bf2..af293899ad9 100644 --- a/src/control-flow-basics/blocks-and-scopes/scopes.md +++ b/src/control-flow-basics/blocks-and-scopes/scopes.md @@ -1 +1,35 @@ -# Scopes +## Scopes and Shadowing + +A variable's scope is limited to the enclosing block. + +You can shadow variables, both those from outer scopes and variables from the +same scope: + +```rust,editable +fn main() { + let a = 10; + println!("before: {a}"); + { + let a = "hello"; + println!("inner scope: {a}"); + + let a = true; + println!("shadowed in inner scope: {a}"); + } + + println!("after: {a}"); +} +``` + +
+ +- Show that a variable's scope is limited by adding a `b` in the inner block in + the last example, and then trying to access it outside that block. +- Shadowing is different from mutation, because after shadowing both variable's + memory locations exist at the same time. Both are available under the same + name, depending where you use it in the code. +- A shadowing variable can have a different type. +- Shadowing looks obscure at first, but is convenient for holding on to values + after `.unwrap()`. + +
From be7ad99bfa382731d8e0b3889456d7d5fa390077 Mon Sep 17 00:00:00 2001 From: Manichand Date: Fri, 16 Feb 2024 23:37:38 +0530 Subject: [PATCH 4/4] Requested changes --- src/SUMMARY.md | 2 +- src/control-flow-basics/blocks-and-scopes/scopes.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 12648b5265a..0f1107bc3d0 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -37,7 +37,7 @@ - [`break` and `continue`](control-flow-basics/break-continue.md) - [Labels](control-flow-basics/break-continue/labels.md) - [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md) - - [Scopes](control-flow-basics/blocks-and-scopes/scopes.md) + - [Scopes and Shadowing](control-flow-basics/blocks-and-scopes/scopes.md) - [Functions](control-flow-basics/functions.md) - [Macros](control-flow-basics/macros.md) - [Exercise: Collatz Sequence](control-flow-basics/exercise.md) diff --git a/src/control-flow-basics/blocks-and-scopes/scopes.md b/src/control-flow-basics/blocks-and-scopes/scopes.md index af293899ad9..d3ba4d3dc4d 100644 --- a/src/control-flow-basics/blocks-and-scopes/scopes.md +++ b/src/control-flow-basics/blocks-and-scopes/scopes.md @@ -1,4 +1,4 @@ -## Scopes and Shadowing +# Scopes and Shadowing A variable's scope is limited to the enclosing block.