liveBook · Manning

Mutable reference

As explained in Code Like a Pro in Rust

A mutable reference is a reference through which the referenced value may be modified.

A mutable reference, written &mut in Rust, lets code modify a value through a borrow. Rust's borrow checker permits one active mutable reference, preventing overlap with immutable references and other mutable references. section 1.2.1

Use mutable references when modification should happen without taking ownership:

  • Pass &mut when a function should modify existing data in place. section 5.4
  • Treat exclusivity as a safety guarantee: mutable and immutable references cannot overlap, reducing unintended data races. section 1.2.1
  • Inspect function inputs and outputs to estimate whether an operation mutates data, creates a copy, or passes ownership. section 5.4

Lowercase in place

An owned mutable string can be changed without returning a newly allocated string: fn lowercased_ascii(mut s: String) -> String { s.make_ascii_lowercase(); s } By contrast, to_lowercase takes an immutable reference and returns a new String, making a copy. Confirm performance-sensitive behavior in documentation and source code rather than relying only on signatures. section 5.4

Table 5.1 Examining Rust core string functions for copies (view table figure)

Function

Description

Copies?

Algorithm

Identified by?

pub fn replace<'a, P>(&'a self, from: P, to: &str) -> String where P: Pattern<'a>,

Replaces all matches of a pattern with another string

Yes

Creates a new string, pushes updated contents into new string, returning the new string and leaving the original string untouched

self parameter is an immutable reference; function returns owned String

pub fn to_lowercase(&self) -> String

Returns the lowercase equivalent of this string slice, as a new String

Yes

Creates a new string, copies each character to the new string, converting uppercase characters to lowercase characters

self parameter is an immutable reference; function returns owned String

pub fn make_ascii_lowercase(&mut self)

Converts this string to its ASCII lowercase equivalent in place

No

Iterates over each character, applying a lowercase conversion on uppercase ASCII characters

Function takes a mutable self reference, modifying the memory in place

pub fn trim(&self) -> &str

Returns a string slice with leading and trailing whitespace removed

No

Uses a double-ended searcher to find the start and end of a substring without whitespace, returning a slice representing the trimmed result

Function returns reference, not an owned string

Compare string functions by their reference parameters, return types, and documented copy behavior. section 5.4

How Code Like a Pro in Rust covers it

How deep it goes
Explains borrow-checker exclusivity and signature-based copy detection, but defers detailed underlying algorithms.
Examples
Uses make_ascii_lowercase for in-place mutation and contrasts it with to_lowercase returning a copied String.
What it assumes
Assumes familiarity with Rust variables and references while reinforcing exclusive borrowing and scope-based memory release.
What you can do after
Apply &mut parameters to implement in-place mutation and diagnose likely copying from function signatures.
How it connects
Connects mutable references with immutable references, borrowing, ownership, the borrow checker, RAII, and concurrency.

Also explained in

Compare how 3 books explain mutable 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.