Im playing around with copying bytes from one array to another and may be slow on the uptake.
I have 5 bytes of 9s and I want to copy bytes into buf and overide its content using the Write trait. So I get a &mut to buf called buf_ref.
let mut buf =[9u8; 5];
let mut buf_ref = &mut buf[..];
When printing buf i need to use buf_ref because I already have a mutable borrow on buf
println!("{:#?}", buf_ref);
//println!("{:#?}", buf);
Now I can copy a 2 and overwrite the first 9. After the write call buf_ref seems to point at buf[1...]. Unwritten is 4 and buf_ref is [9, 9, 9, 9]. I can't print buf which should be [2, 9, 9, 9, 9] because println whould be a immutable borrow of buf which is already borrowed mutual
buf_ref.write(&[2u8; 1])?;
let mut unwritten = buf_ref.len();
println!("Unwritten:{:#?}", unwritten);
println!("{:#?}", buf_ref);
//println!("{:#?}", buf);
Next step is to overwrite the snd and third 9 with 0. Unwritten is now 2 and buf_ref is [9, 9]
buf_ref.write(&[0u8; 2])?;
unwritten = buf_ref.len();
println!("Unwritten:{:#?}", unwritten);
println!("{:#?}", buf_ref);
Now I can print buf and it is [2, 0, 0, 9, 9] like expected.
println!("{:#?}", buf);
I am stuck finding a way to print buf after each of the write call. Any help welcome.