#Borrow checker and Write for &mut [u8]

10 messages · Page 1 of 1 (latest)

supple ibex
#

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.

supple ibex
#

Now it works 🙂

    let mut buf =[9u8; 5];
    println!("Initial buf {:#?}", buf);
    let buf_len = buf.len();
    let mut unwritten  = buf.len();
    
    let mut position = buf_len - unwritten;
    let mut buf_ref = &mut buf[position ..];
    buf_ref.write(&[2u8; 1])?;
    unwritten = buf_ref.len();
    println!("Unwritten:{:#?} ", unwritten);
    println!("Buf after write from position[{:#?}]:{:#?}", position, buf);
    
    position = buf_len - unwritten;
    buf_ref = &mut buf[position ..];
    buf_ref.write(&[0u8; 2])?;
    unwritten = buf_ref.len();
    println!("Unwritten:{:#?} ", unwritten);
    println!("Buf after write from position[{:#?}]:{:#?}", position, buf);
    
    position = buf_len - unwritten;
    buf_ref = &mut buf[position ..];
    buf_ref.write(&[7u8; 1])?;
    unwritten = buf_ref.len();
    println!("Unwritten:{:#?} ", unwritten);
    println!("Buf after write from position[{:#?}]:{:#?}", position, buf);
safe ginkgo
#

Writing to &mut [u8] works the way it does because it has no other way to track the position to write to, but Cursor has a separate, explicit position field.

supple ibex
#

yes, much easier...thx

use std::io::Cursor;
    use std::io::{SeekFrom};
    let mut cur = Cursor::new([9u8; 5]);
    println!("{:#?}", cur);
    cur.seek(SeekFrom::Start(2));
    cur.write(&[7u8; 2]);
    println!("{:#?}", cur);
civic raptor
#

note that you can write to a &mut &mut[u8] just fine:

#

?play

use std::io::Write;
let mut buf = [0u8; 10];
let mut cursor = &mut buf[..];
write!(cursor, "foo");
write!(cursor, "bar");
println!("{:?}", buf);
wary boughBOT
#
[102, 111, 111, 98, 97, 114, 0, 0, 0, 0]```
civic raptor
#

(maybe i misunderstood the question, if so, cursor is great too)

#

(oh you want to print the buffer, I'm blind, move on 😅 )