cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
503 stars 82 forks source link

References and Borrowing section uses rust specific definition of path prior to defining it. #176

Open rs333 opened 3 months ago

rs333 commented 3 months ago

URL to the section(s) of the book with this problem: ch04-02-references-and-borrowing.html

Description of the problem: The revised version of ch04-02-references-and-borrowing.html introduces the term Paths to mean

 More generally, permissions are defined on paths and not just variables. A path is anything you can put on the left-hand side of an assignment. Paths include:

Variables, like a.
Dereferences of paths, like *a.
Array accesses of paths, like a[0].
Fields of paths, like a.0 for tuples or a.field for structs (discussed next chapter).
Any combination of the above, like *((*a)[0].1).

This seems to be a rust specific overload of the common meaning to the term path. In its present form, it can cause cognitive dissidence for the reader. (At least one and probably more :) Additionally, it appears to be technically not correct because while some paths can be put on the left hand side of an assignment, not all paths can be put on the left hand side of an assignment. E.g. I don't believe a path referring to a module item can be on the left hand side of an assignment.

Suggested fix:

  1. Define path prior to it's use using the definition from the Rust Reference

A path is a sequence of one or more path segments logically separated by a namespace qualifier (::). If a path consists of only one segment, it refers to either an item or a variable in a local control scope. If a path has multiple segments, it always refers to an item.

  1. Consider adding a modifier to the term path in this section to make it clear which type they are referring to. E.g. Assignable paths.
  2. In the text shown int he Description of the Problem, revise the text to indicate that permissions are defined on assignable paths which are paths that can be on the left-hand side of an assignment. Something like
    
    More generally, permissions are defined on assignable paths which are paths that can be put on the left-hand side of an assignment.  Examples of assignable paths include:

Variables, like a. Dereferences of assignable paths, like a. Array accesses of assignable paths, like a[0]. Fields of assignable paths, like a.0 for tuples or a.field for structs (discussed next chapter). Any combination of the above, like ((*a)[0].1).



That said, given this [place-expressions-and-value-expresions](https://doc.rust-lang.org/stable/reference/expressions.html#place-expressions-and-value-expressions) text from the Rust Reference, it may be more appropriate to call it a place expression vice a path.  
willcrichton commented 1 week ago

Hi, this is a good point. These two meanings of path are conflicting. I'll note that the borrow-checker-oriented meaning of "path" is based on @nikomatsakis's borrow checker formalizations. I'm going to discuss with him what he recommends for terminology here.