daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Error;
  2.  
  3. trait WriteBytes<'a> {
  4.     fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
  5.     where
  6.         'b: 'a;
  7. }
  8.  
  9. impl<'a> WriteBytes<'a> for &'a mut [u8] {
  10.     fn write_byte<'b>(&'b mut self, byte: u8) -> Result<(), Error>
  11.     where
  12.         'b: 'a,
  13.     {
  14.         match self.split_first_mut() {
  15.             Some((first, rest)) => {
  16.                 *first = byte;
  17.                 *self = rest;
  18.                 Ok(())
  19.             }
  20.             None => Err(Error),
  21.         }
  22.     }
  23. }
  24.  
  25. fn main() {
  26.     let mut array = [0, 1, 2, 3, 4];
  27.     let mut buf: &mut [u8] = &mut array;
  28.     buf.write_byte(5);
  29.     println!("{:?}", buf);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top