Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why not simply:

    let mut data = Vec::with_capacity(sz);
    data.extend(&buf[..sz]);
Vec::extend extends a container from an iterable. A Vec/slice is iterable.

And from the doc:

> This implementation is specialized for slice iterators, where it uses copy_from_slice to append the entire slice at once.

Of course this trivial example could also be written as:

    let mut data = buf.clone();


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: