Mastodon Feed: Post

Mastodon Feed

rust@octodon.social ("Rust tips") wrote:

If you want to allocate a buffer of uninitialized memory in #Rustlang, the easiest way is `Vec::with_capacity(len)`.

Get the buffer with `vec.as_mut_ptr()` for C APIs, or `&mut vec.spare_capacity_mut()[..len]` for Rust. Spare capacity needs extra slicing, because `Vec` reserves the right to have a larger capacity than requested. Once the buffer is filled, use `vec.set_len(len)` to make the data safe to access.