This content originally appeared on DEV Community and was authored by tomomomomo
Today I’ve learned the difference between &str and String.
String is a wrapper of Vec, with capacity, len, and ptr.
&str is a reference to an existing string. It’s unchangeable.
To change it, you need a new String with a new heap area.
&str has no overhead.
If I want to make a Web app, there are so many places to use String.
But when I want to make an AI model, I might use &str more often, I think.
When I was using Laravel, I didn’t recognize the existence of heap and stack. The two seem very interesting and attractive.
Yesterday, a very kind person told me about Rust’s two string types.
So I read about them in the book.
It’s a very nice start.
Thank you!
This content originally appeared on DEV Community and was authored by tomomomomo

tomomomomo | Sciencx (2025-03-08T23:45:23+00:00) My First Rust Lesson: String vs &str. Retrieved from https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.