'Is converting between String & Vec<u8> a zero-op in --release binary
String & Vec<u8> are almost the same to me, though String guarantees to have valid UTF-8 content, which is often useful.
However, being in unsafe context, does it really take any machine operation to cast between two of them if no check is performed?
Consider these two functions:
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8, Global>) -> Stringpub fn into_bytes(self) -> Vec<u8, Global>
They're both consuming input, so a compiler has theoretically no need to render a new object in memory.
Solution 1:[1]
You can take a look at the source code to check that out:
pub fn into_bytes(self) -> Vec<u8> {
self.vec
}
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
String { vec: bytes }
}
So yes.
However, I agree with @IanS that you should not use unsafe unless profiled.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chayim Friedman |
