std-method

Rust Cow::into_owned

Consume the Cow and return an owned payload. A borrowed variant obtains an owned representation; an owned variant transfers its existing value. This is useful when a caller must retain data independently of the input lifetime. Expected output (not executed): ticket Why: The resulting String no longer borrows source. Common mistake: Expecting to keep using the consumed Cow binding.

Formal Syntax & Structure

rust syntaxrust
cow.into_owned() -> B::Owned

Working Example

Example Snippetrust
use std::borrow::Cow;fn main() {    let result;    { let source = String::from("ticket");      let view: Cow<'_, str> = Cow::Borrowed(&source);      result = view.into_owned(); }    println!("{result}");}
Rust Reference