Skip to content

Ch.21.2. Incorrect expression #4609

Description

@ww1436354649
  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • 21.2

URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch21-02-multithreaded.html

Description of the problem:
The code in Listing 21-20 that uses let job = receiver.lock().unwrap().recv().unwrap(); works because with let, any temporary values used in the expression on the right-hand side of the equal sign are immediately dropped when the let statement ends. However, while let (and if let and match) does not drop temporary values until the end of the associated block. In Listing 21-21, the lock remains held for the duration of the call to job(), meaning other Worker instances cannot receive jobs.

Suggested fix:
Not because of holding the lock during job(), but because of holding the lock during waiting for receipt (recv() blocking).
Rust's temporary value rule ensures that in a while let, temporary values generated by expressions (such as MutexGuard) do not persist until the end of the code block, but are immediately discarded after pattern matching is complete.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions