liveBook · Manning

Scope

As explained in Refactoring to Rust

A region of source code within which a binding is accessible and whose end can trigger the dropping of owned values.

A scope is a region of source code where a binding is accessible. When its owner leaves scope, Rust drops the owned value. section 2.1

Scope matters because ownership follows it:

  • Each value has one variable owner at a time. section 2.1
  • Passing an owned value to admire_art moves it, so the original binding cannot be used again. section 2.1

A moved artwork

After admire_art(art1), art1 has been moved into the function. Calling admire_art(art1) again produces a use-of-moved-value error rather than silently reusing the value. section 2.1

How Refactoring to Rust covers it

How deep it goes
Defines scope through ownership and moves, but does not develop borrowing or detailed lexical-scope mechanics.
Examples
Uses artwork values passed to admire_art and a repeated art1 call that triggers a compiler error.
What you can do after
Diagnose use-of-moved-value errors by tracking ownership across function calls and scopes.
How it connects
Connects scope with ownership, moves, and borrowing.

Also explained in

Compare how 21 books explain scope →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.