Choosing &str, String, and Cow for Rust Text APIs
Rust has several common ways to represent UTF-8 text, and choosing between &str, String, and Cow<'a, str> is fundamentally an ownership decision.
The best API is usually the one that asks callers for the least ownership it needs and returns ownership only when the result requires it.
Use &str when you only need to read text A string slice borrows UTF-8 text owned elsewhere:
Copy fn is_blank(value: &str) -> bool { value.trim().is_empty() } This accepts borrowed views into a String, string literals, and other string slices without taking ownership or allocating.