liveBook · Manning

Reference

As explained in Code Like a Pro in Rust

An immutable reference is a read-only reference through which the referenced value cannot be modified.

An immutable reference is read-only access to a value. Rust permits multiple immutable references, but prevents them from overlapping with a mutable reference; the borrow checker validates these rules at compile time. section 1.2.1

Use immutable references to reason about safety and data movement:

  • Treat multiple immutable references as compatible shared reads; Rust rejects simultaneous shared mutable access. section 1.2.1
  • Inspect function signatures: a reference input returning a slice or reference is unlikely to copy, while an owned return may create one. section 5.4
  • Verify uncertain behavior in documentation and source code, especially when performance depends on copying versus in-place mutation. section 5.4

Read-only input versus in-place mutation

The signatures `fn func(&self) -> &str` and `fn func(&mut self)` communicate different costs and permissions: the first commonly returns borrowed data without copying, while the second may modify data in place. In contrast, `to_lowercase(&self) -> String` returns a new owned string; `make_ascii_lowercase(&mut self)` changes the existing string. section 5.4

Figure 1.3 Rust borrow checker rules Venn diagram
CH01_F03_Matthews

Use the borrow-checker diagram to compare the allowed combinations of immutable and mutable references. section 1.2.1

How Code Like a Pro in Rust covers it

How deep it goes
Explains reference exclusivity, scope checking, and copy-related signatures, stopping short of exhaustive language-reference treatment.
Examples
Demonstrates string slices, lowercase conversion, owned strings, and in-place ASCII mutation.
What you can do after
Diagnose likely copying and mutation behavior by reading Rust function inputs and outputs, then verify uncertain cases in source.
How it connects
Connects immutable references to the borrow checker, mutable references, ownership, RAII, memory safety, and concurrency.

Also explained in

Compare how 3 books explain reference →

Related concepts

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