Ch04: clarify the difference between variable scope and reference scope

Open Beginner friendly
#4,706 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
76/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Quiet
Tech stack
rust
Domain
documentation

Research direction

Start with the linked Chapter 4-1 “What Is Ownership” and Chapter 4-2 “References and Borrowing” sections, especially the existing variable-scope and reference-scope wording. Clarify that the two uses of “scope” refer to different concepts, then verify the rendered text remains beginner-friendly and consistent with the surrounding explanation.

Written by the indexing model from the issue text.

Description

Difference between variable scope and reference scope

Purpose: Suggesting a clarified definition on scopes (Ch 4-2)

Suggested fix: Adding a detail that variable's scope and a reference's scope are used on different context first.

Such as:

Existing

Note that a reference’s scope starts from where it is introduced and continues through the last time that reference is used.

Suggestion (One possible approach)

Note that a reference's scope is different from the variable scope we discussed earlier. While a variable's scope extends to the end of the block where it is declared, a reference's scope starts from where it is introduced and continues through the last time that reference is used.


Details:

Hello

I'm learning Rust with this document. I'd like to suggest an update to Chapter 4-1 or 4-2.

On chapter 4, there's no explicit definition of difference between variable scope and reference scopes. It could be confused for beginners who are more familiar with other programming languages.

A scope is the range within a program for which an item is valid.
ch04-01 - what is ownership

This defninition describes a lexical scope, which is familiar from other programming languages. In that regard, description on variable is clear.

(1) As always, we can use curly brackets to create a new scope,
(2) Note that a reference’s scope starts from where it is introduced and continues through the last time that reference is used.
ch04-02 - references and borrowing

However, the word 'scope' is used differently for references. On the chapter 4-2, the same word 'scope' is using on different context. In (1) variable's scope is defined by curly bracket, but according to (2) a reference's scope is defined by when the references is last used. This implicit shift in meaning can confuse beginners coming from other languages.

In my case, after after looking into this implicit definition, I found this describes what is known as non-lexical lifetimes(NLL). Technically variable scope and reference scopes are different, but it is still ambiguous since the system is different from other languages.

To maintain this document as beginner friendly I understand the team trying to avoid using deep explanation, however this is a clarification, not an addition of new concept.

Related Issue:

#3485 is about the curly-braces example itself; this issue is about the unlabeled semantic shift in the word 'scope', explicitly noting the difference between variable scope and reference scope before the existing explanation.

Dominant language
Rust
Stars
18.3k
Forks
4.1k
Avg merge
14m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/book

All issues in rust-lang/book

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.