3 ms·
I don’t have a full explanation of it. But you always use `&str` because you always want to borrow (or lend out?) the string slice. The doc says: A &str i
by avgcorrection 4y ago
I don’t have a full explanation of it. But you always use `&str` because you always want to borrow (or lend out?) the string slice.
The doc says:
A &str is made up of two components: a pointer to some bytes, and a length.
This must mean that `str` is the string proper. But you can’t use it like `String` since `str` is not owned. So passing `str` to a function doesn’t make sense. Another difference is that `str` has no capacity since it’s a slice and not a vector (seems like `String` is implemented as `Vec[u8`).
I’ve always found it weird that if you make a short word for “String” and use a lower-case letter then it becomes the borrowed version.
- steveklabnik 4y ago> I’ve always found it weird that if you make a short word for “String” and use a lower-case letter then it becomes the borrowed version. The reason for this is that String is a standard library type, but str is a primitive type in the language. So they follow those respective conventions.