Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Error;
- trait WriteBytes<'a> {
- fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
- where
- 'b: 'a;
- }
- impl<'a> WriteBytes<'a> for &'a mut [u8] {
- fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
- where
- 'b: 'a,
- {
- match self.split_first_mut() {
- Some((first, rest)) => {
- *first = byte;
- *self = rest;
- Ok(())
- }
- None => Err(Error),
- }
- }
- }
- fn main() {
- let mut array = [0, 1, 2, 3, 4];
- let mut buf: &mut [u8] = &mut array;
- buf.write_byte(5);
- println!("{:?}", buf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement