Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify a part of bigger slice or Vec with another smaller slice?

Tags:

slice

rust

How can I correctly implement this code below?

let mut bigger: [u8; 100] = [0u8; 100];
let smaller: [u8; 3] = [1, 2, 3];

// do something like: 
// bigger[0..3] = smaller;
like image 875
Visual Eugen Avatar asked Nov 19 '25 07:11

Visual Eugen


1 Answers

Use copy_from_slice:

bigger[0..3].copy_from_slice(&smaller);

(Playground)

like image 143
John Kugelman Avatar answered Nov 21 '25 21:11

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!