What is the Cow str type

Cow<'a, str> is a Rust enum that efficiently handles both borrowed string slices and owned strings, cloning only when modification is required.

There is no Cow str type in Rust; the correct type is Cow<'a, str>, which is an enum from the standard library that represents either an owned String or a borrowed string slice. Use Cow::Borrowed for existing slices to avoid allocation, or Cow::Owned when you need to modify the string.

use std::borrow::Cow;

let borrowed: Cow<str> = Cow::Borrowed("hello");
let mut owned: Cow<str> = Cow::Owned(String::from("world"));
owned.to_mut().push_str("!");