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.
&st…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » My First Rust Lesson: String vs &str." tomomomomo | Sciencx - Saturday March 8, 2025, https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/
HARVARD
tomomomomo | Sciencx Saturday March 8, 2025 » My First Rust Lesson: String vs &str., viewed ,<https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/>
VANCOUVER
tomomomomo | Sciencx - » My First Rust Lesson: String vs &str. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/
CHICAGO
" » My First Rust Lesson: String vs &str." tomomomomo | Sciencx - Accessed . https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/
IEEE
" » My First Rust Lesson: String vs &str." tomomomomo | Sciencx [Online]. Available: https://www.scien.cx/2025/03/08/my-first-rust-lesson-string-vs-str/. [Accessed: ]
rf:citation
» My First Rust Lesson: String vs &str | tomomomomo | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.