From 67b7cedc2a44058e55664e9a23e6d988f053b243 Mon Sep 17 00:00:00 2001 From: Edmund Lodewijks Date: Mon, 8 Jun 2026 20:48:11 +0200 Subject: [PATCH] book: ch03-01(shadowing): Clarify RHS evaluation order Explicitly state that the right-hand side of a shadowing let binding is evaluated before the new variable comes into existence. The original text implied this by saying "taking the original value", but did not make the mechanism clear, which might be confusing. --- src/ch03-01-variables-and-mutability.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ch03-01-variables-and-mutability.md b/src/ch03-01-variables-and-mutability.md index ed15afc0fd..285ccaa3aa 100644 --- a/src/ch03-01-variables-and-mutability.md +++ b/src/ch03-01-variables-and-mutability.md @@ -140,12 +140,15 @@ use of the `let` keyword as follows: ``` This program first binds `x` to a value of `5`. Then, it creates a new variable -`x` by repeating `let x =`, taking the original value and adding `1` so that -the value of `x` is `6`. Then, within an inner scope created with the curly -brackets, the third `let` statement also shadows `x` and creates a new -variable, multiplying the previous value by `2` to give `x` a value of `12`. -When that scope is over, the inner shadowing ends and `x` returns to being `6`. -When we run this program, it will output the following: +that is also called `x`, by repeating `let x =`. The program takes the original +value of `x` and adds `1`, so that the value of the new variable `x` is now `6`. +It is important to understand that the right-hand side is evaluated first `(5 + 1)` +before this value is assigned to the new variable `x`. Then, within an inner +scope created with the curly brackets, the third let statement also shadows `x` +and creates a new variable, evaluating the right-hand side first `(6 * 2)` +before assigning `12` to the new `x`. When that scope is over, the inner +shadowing ends and `x` returns to being `6`. When we run this program, it will +output the following: ```console {{#include ../listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt}}