Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating byte buffers in rust [duplicate]

What is the most efficient way to get access to &mut [u8]? Right now I'm borrowing from a Vec but it would be easier to just allocate the buffer more directly.

The best I can do right now is to preallocate a vector then push out its length but there is no way this is idiomatic.

    let mut points_buf : Vec<u8> = Vec::with_capacity(points.len() * point::POINT_SIZE);
    for _ in (0..points_buf.capacity()) {
        points_buf.push(0);
    }
    file.read(&mut points_buf[..]).unwrap();
like image 997
xrl Avatar asked Jun 10 '15 07:06

xrl


1 Answers

You could just create the vec with a given size directly:

vec![0; num_points]

Or use iterators:

repeat(0).take(num_points).collect::<Vec<_>>()
like image 127
DK. Avatar answered Oct 21 '22 13:10

DK.