Topic
String vs &str
Rust has two string types. String owns its data on the heap and can grow. &str is a borrowed slice — a read-only view into existing text. Knowing which one to use and how to convert between them is one of the most common early Rust questions.
Quick Reference
ConceptWhat it means
StringHeap-allocated, owns its data, can grow and shrink, mutable with mut
&strString slice — an immutable borrowed view into a string. Read-only.
String literalText in double quotes, type &str, stored in read-only memory at compile time
push_strAppends a string slice (&str) to a String
pushAppends a single character to a String
to_string() / String::from()Convert &str to String — copies text into new heap memory
&s on a StringConverts String to &str automatically when a function expects it
TermWhat it meansWhen you use it
StringHeap-allocated, owns its data, can grow and shrinkWhen you need to build or modify text at runtime
&strImmutable borrowed view into a stringWhen you only need to read text, not own it
String literalText in double quotes, type &str, baked into the binaryWhen writing fixed text directly in your code
push_strAppends a string slice to a StringAdding multiple characters at once to a String
pushAppends a single character to a StringAdding one character at a time
to_string() / String::from()Convert &str to owned StringWhen you need to own the text data
&s / as_str()Convert String to &strPassing a String to a function that expects &str
+Concatenates String with &str — the left String is consumedJoining two strings into one new value