Cow is a clone-on-write smart pointer for data that should remain borrowed unless mutation is needed. It stores either Borrowed or Owned data, requires ToOwned, and dereferences to its contents. Calling to_mut() provides mutable access, cloning into owned data when necessary so changes do not alter the source. section 9.7
Listing 9.6 Basic use of Cow
use std::borrow::Cow;
let cow_say_what = Cow::from("The cow goes moo");
dbg!(&cow_say_what);
let cows_dont_say_what =
cow_say_what #1
.clone() #1
.to_mut() #1
.replace("moo", "toot"); #1
dbg!(&cow_say_what); #2
dbg!(&cows_dont_say_what); #3
This example clones the Cow pointer, then replaces “moo” through to_mut(); the original value remains unchanged. section 9.7
A transformation can accept and return Cow, mutating only when needed. For a public API, wrap Cow in a struct and expose methods rather than the implementation detail; a Cow-backed list can add items while preserving earlier values. section 9.7

