My question is: is there a way to do this on a &mut [u8] ? I.e. the mut slice already exists, and I want to use the .write_### directly on it, (instead of writing into a vec then copying it over).
No. Sorry, how does this even work? With a Vec<u8>, when we do a .write_### it can push to the end. With a &mut [u8] how is writing to it possible unless we explicitly store a 'cursor' (which afaik &mut [u8] does not) ?
I.e. suppose we have:
let x: &mut [u8] = ...;
x.write_i32(0);
x.write_i32(2);
how does it know, on the 2nd write, that it should be 4 bytes after the first, given that &mut [u8] has no internal store of a 'cursor' ?
It works on &mut [u8] not on [u8] (&mut self is &mut &mut [u8]), so (as the docs say) it updates the slice as it goes so that it covers the unwritten portion.
(Meant to finish the example before my first post, oops.)