String vs &str for owned vs borrowed text

Marcus Chen Jan 2026
1 tab
fn greet(name: &str) {
    println!("Hello, {}", name);
}

fn main() {
    let owned = String::from("Alice");
    let borrowed: &str = "Bob";

    greet(&owned);
    greet(borrowed);
}
1 file · rust Explain with highlit

String is an owned, growable UTF-8 string on the heap. &str is a borrowed string slice, often a view into a String or a string literal. I use String when I need to own, modify, or build strings. I use &str for function parameters (accepting both String and literals) and when I don't need ownership. Converting: String::from("...") or .to_string() creates a String; &my_string or &my_string[..] creates a &str. Most string APIs accept &str to be flexible. Understanding the difference is key to avoiding unnecessary allocations and ownership issues. This is one of the first patterns new Rustaceans learn.