Convert slice to u32 in one line

Hi folks, how can I make the following code as simple as possible (removing the intermediate variables):

// I want to get rid of this n
let n: [u8;4] = buffer[4..8].try_into()?;
let res = u32::from_ne_bytes(n); // I want to use res

Thanks in advance!

It works beautifully on one line (playground):

let res = u32::from_ne_bytes(buffer[4..8].try_into()?);
4 Likes

Oh, that's truly great!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.