From f1459c54e1925e2cd06fe36548e9e3c43ef28c73 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Thu, 6 Feb 2025 12:39:27 -0800 Subject: [PATCH] Remove slide on shadowing (#2596) --- src/SUMMARY.md | 1 - src/control-flow-basics/blocks-and-scopes.md | 10 ++++-- .../blocks-and-scopes/scopes.md | 35 ------------------- 3 files changed, 8 insertions(+), 38 deletions(-) delete mode 100644 src/control-flow-basics/blocks-and-scopes/scopes.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 06e3cc20..f19fec02 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -30,7 +30,6 @@ - [Solution](types-and-values/solution.md) - [Control Flow Basics](control-flow-basics.md) - [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md) - - [Scopes and Shadowing](control-flow-basics/blocks-and-scopes/scopes.md) - [`if` Expressions](control-flow-basics/if.md) - [`match` Expressions](control-flow-basics/match.md) - [Loops](control-flow-basics/loops.md) diff --git a/src/control-flow-basics/blocks-and-scopes.md b/src/control-flow-basics/blocks-and-scopes.md index 93400524..b94a5636 100644 --- a/src/control-flow-basics/blocks-and-scopes.md +++ b/src/control-flow-basics/blocks-and-scopes.md @@ -4,8 +4,6 @@ minutes: 5 # Blocks and Scopes -## Blocks - A block in Rust contains a sequence of expressions, enclosed by braces `{}`. Each block has a value and a type, which are those of the last expression of the block: @@ -19,14 +17,22 @@ fn main() { z - y }; println!("x: {x}"); + // println!("y: {y}"); } ``` If the last expression ends with `;`, then the resulting value and type is `()`. +A variable's scope is limited to the enclosing block. +
- 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`. +- Demonstrate that attempting to access `y` outside of its scope won't compile. + +- Values are effectively "deallocated" when they go out of their scope, even if + their data on the stack is still there. +
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 545e86f1..00000000 --- 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 variables' - 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()`. - -