Skip to content

Ch04: clarify the difference between variable scope and reference scope #4706

@rltq

Description

@rltq

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions