3 ms·
But letting aside the unicode reason, wouldn't be the answer the following? &str is a slice of something that is const (it can be in flash, for example). Like:
by scoutt 5y ago
But letting aside the unicode reason, wouldn't be the answer the following?
&str is a slice of something that is const (it can be in flash, for example). Like:
const char* str = "cat";
str[0] = 'C'; // <--- wrong
So, because of this, an extra allocation and copy is required.
(read the above answer like it's coming from an embedded C developer learning Rust as I am).
- steveklabnik 5y agoSure, there are multiple reasons. str[0] doesn't exist either, that would fail regardless of the mutability thing, but you're also right that the &str is immutable, and that would fail.