std-method

Rust Cow::to_mut

Get mutable access to the owned representation. If the Cow currently borrows data, the method first obtains an owned version. Existing owned data does not require that conversion. Expected output (not executed): draft saved / draft Why: For str, conversion produces a String; mutation leaves the source unchanged. Common mistake: Calling to_mut before checking whether an edit is necessary.

Formal Syntax & Structure

rust syntaxrust
cow.to_mut() -> &mut B::Owned

Working Example

Example Snippetrust
use std::borrow::Cow;fn main() {    let source = String::from("draft");    let mut label: Cow<'_, str> = Cow::Borrowed(&source);    label.to_mut().push_str(" saved");    println!("{label} / {source}");}
Rust Reference