What are the differences between Rust's `String` and `str`?
String is the dynamic heap string type, like Vec: use it when you need to own or modify your string data.
String is the dynamic heap string type, like Vec: use it when you need to own or modify your string data.
str is an immutable sequence of UTF-8 bytes of dynamic length somewhere in memory. Since the size is unknown, one can only handle it behind a pointer. This means that str most commonly appears as &str: a reference to some UTF-8 data, normally called a "string slice" or just a "slice".
Prefer &str as a function parameter or if you want a read-only view of a string.
String when you want to own and mutate a string.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Comments
Post a Comment