I have a Vec<u8>
of bytes read from a file - the bytes are a text format (probably UTF-16 or some other silly 2 byte format) and I want to turn it into UTF-8.
let title = Vec::from_iter(bytes.take(title_length));
// Some Vec<u8> to &[u16] magic
let title = String::from_utf16_lossy(title);
Currently I'm using this rather dirty code:
let title: &[u16] = unsafe { std::slice::from_raw_parts(title_data.as_ptr(), title_data.len()) };
While this should work I'm getting errors probably due to the take()
call:
error: mismatched types:
expected `*const u16`,
found `*const core::result::Result<u8, std::io::error::Error>`
(expected u16,
found enum `core::result::Result`) [E0308]
Should I map
the take
iterator or something?