From b9e2abc5b0313c3012b8ff549408003b8ebf34ff Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Wed, 29 Jan 2025 16:36:55 -0800 Subject: [PATCH 1/2] Remove slide on shadowing --- src/SUMMARY.md | 1 - .../blocks-and-scopes/scopes.md | 35 ------------------- 2 files changed, 36 deletions(-) delete mode 100644 src/control-flow-basics/blocks-and-scopes/scopes.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 06e3cc2050e8..f19fec02a69b 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/scopes.md b/src/control-flow-basics/blocks-and-scopes/scopes.md deleted file mode 100644 index 545e86f19c52..000000000000 --- 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()`. - -
From 3673125f05902278a0b60c7f1f0deb58d4cd9c79 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Wed, 29 Jan 2025 17:09:22 -0800 Subject: [PATCH 2/2] Move explanation of scopes to blocks slide Which was, confusingly, already named "Blocks and Scopes". --- src/control-flow-basics/blocks-and-scopes.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/control-flow-basics/blocks-and-scopes.md b/src/control-flow-basics/blocks-and-scopes.md index 934005249e85..b94a56366c3e 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. +