Good Code
The good version accepts &str, which works with String, string slices, and literals without moving the caller's value. The function creates a new normalized value only for the returned title.
Lesson 01
Use borrowed inputs when a function only reads data, so callers keep ownership of their values.
pub fn normalize_title(title: &str) -> String {
// Borrowing says this function reads the title without consuming it.
title.trim().replace(char::is_whitespace, " ")
}pub fn normalize_title(title: String) -> String {
// Taking String forces callers to give up ownership for a read-only task.
title.trim().replace(char::is_whitespace, " ")
}The good version accepts &str, which works with String, string slices, and literals without moving the caller's value. The function creates a new normalized value only for the returned title.
The bad version takes String even though it only reads the input. Callers lose ownership and may clone strings just to keep using the title after the call.