DEV Community

tomomomomo
tomomomomo

Posted on

My First Rust Lesson: String vs &str

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!

Top comments (0)