3 ms·
Maybe the problem you've got is that you actually haven't understood what the alternative you don't like even is in practice ? // Assuming buffer is a mut [u
by tialaramex 4y ago
Maybe the problem you've got is that you actually haven't understood what the alternative you don't like even is in practice ?
// Assuming buffer is a mut [u8] and text is the text value we'd like to write into it
// if we've got some string, just let text = string.as_bytes();
if text.len() > buffer.len() {
// As with snprintf we need to decide what to do if it won't all fit
// ... for example let's just truncate it
let truncated = &text[..buffer.len()];
buffer.copy_from_slice(truncated);
} else {
let (left, _) = buffer.split_at_mut(text.len());
left.copy_from_slice(&text);
}
As I thought I'd explained, the yak shaving problems for your C-style strings are on the other side, reading this structure. With pointer + length strings this is no trouble but with a 0-terminated string you have to conjure that sentinel from somewhere, maybe allocating in the process. Slower and uglier.