on-internet
← Community

Zero-copy reference a slice of bytes by a step

Stack Overflow ·

I have a large Vec<u8> representing the raw pixel values of a gray image. Since the pixel format is actually 16-bit width, two u8 makes one pixel. Now I'd like to use the image as if it was 8-bit width, so I only take the higher 8-bit part of a pixel. As an instance, given a raw vec v = vec![0,1,2,3,4,5], how can I view it by a step 2 to a reference view : &[u8] (without the need to copy them into a new Vec, for performance), such that view can be used equivalent to &[0,2,4]. Thanks very much.

Comments

Sign in or sign up to comment and upvote.

No comments yet.