Search⌘ K

The Type of Owned Strings

Understand the Rust owned string type, String, and its full namespace path std::string::String. Learn how Rust's compiler reports type mismatches involving strings and how to correctly annotate string variables with their full types.

We'll cover the following...

The type of an owned string is String. You can test this by modifying the program above to have a type annotation, e.g.,

Rust 1.40.0
fn main() {
let first_name = "Michael".to_owned();
let last_name = " Snoyman";
let full_name: String = first_name + last_name;
println!("Full name is {}", full_name);
}

However, when you get a type error, the compiler will usually give you a slightly more verbose name. If I make a mistake and say i32 instead of String, I’ll get this error message:

 ...