Idimoatic way to create a reference to a slice


I want to get a mutable reference to a slice and write a number to it using byteorder crate.
So I should write

(&mut buf[offset..offset + 2]).write_u16::<LittleEndian>(some_number);

Is there a more neat way to write something like this?


This seems like a pretty good way to do this to me. Depending on what you’re doing, you might want to use a chunks_mut iterator or split_at_mut.


See also


You can avoid the implicit dereference in the buf[index] notation by calling index_mut directly

buf.index_mut(offset..offset + 2).write_u16::<LittleEndian>(some_number);

though it’s not shorter.


It looks better for me because there is no need to return a caret while typing to close parenthesis.