Cow
Cow is Rust’s standard-library clone-on-write type, representing either borrowed or owned data and creating owned data when necessary.
Learn Rust in a Month of Lunches
Cow is a Rust standard-library clone-on-write enum that can represent either borrowed data or owned data and create an owned copy when necessary.
- How it is used
- Chooses between borrowed and owned values when a function or structure may need either representation.
- How deep it goes
- Explains variants, generics, lifetimes, ToOwned, dynamically sized types, matching, and conversion methods.
- Examples
- Uses conditional message generation and User names to contrast borrowed values with allocated owned values.
- What you can do after
- Apply it via std::borrow::Cow to accept, return, or store borrowed and owned data in one type.
Idiomatic Rust
Cow is Rust's standard-library copy-on-write smart pointer that can represent either borrowed data or owned data.
- How it is used
- Uses clone-on-write to preserve source data while allowing mutations to work on an owned value.
- How deep it goes
- Defines Borrowed and Owned variants, ToOwned bounds, Deref, and to_mut cloning without detailing implementation internals.
- Examples
- Demonstrates string replacement, a conditional loud_moo transformation, and a Cow-backed list that preserves earlier values.
- What you can do after
- Apply it via Rust’s standard library to mutate a Cow-backed value while leaving the source unchanged.
Related concepts
- copy-on-writeimplements · Copy-on-write is a technique that shares data until modification requires creating an independent copy, preserving the original for existing users.
- Derefimplements · Deref is a Rust standard-library trait that defines how a value is dereferenced to access a target value and enables dereference coercions.
- smart pointerkind of · A smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
- enumkind of · An enum is a type or language construct whose values are selected from a finite set of named alternatives, which may carry associated data.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

