Convert array of u32 to Vec in Rust In Rust, I want to take an array of u32 values, convert each to four bytes in big endian, and concatenate them to yield a Vec&lt;u8&gt; result. Example:
let input: [u32; 5] = [
    0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0];

(... Do some data conversion work ...)

let output: Vec&lt;u8&gt; = vec![
    0x67, 0x45, 0x23, 0x01,
    0xEF, 0xCD, 0xAB, 0x89,
    0x98, 0xBA, 0xDC, 0xFE,
    0x10, 0x32, 0x54, 0x76,
    0xC3, 0xD2, 0xE1, 0xF0,
];

I have several pieces of working code that all give the same answer.
Algorithm A (for-loop):
let mut output = Vec::&lt;u8&gt;::new();
for val in &amp;input{
    output.extend_from_slice(&amp;val.to_be_bytes());
}

Algorithm B (for-each):
let mut output = Vec::&lt;u8&gt;::new();
input.iter().for_each(|val| output.extend_from_slice(&amp;val.to_be_bytes()));

Algorithm C (fold-append):
let output = input.iter().fold(Vec::&lt;u8&gt;::new(),
    |mut acc, val| {acc.extend_from_slice(&amp;val.to_be_bytes()); acc});

Algorithm D (flat-map):
let output: Vec&lt;u8&gt; = input.iter().flat_map(|val| val.to_be_bytes().to_vec()).collect();

Is there an even shorter, clearer way to accomplish the task? Maybe algorithm D's to_vec() could be avoided somehow?
Speed is not a concern because this code only executes a few times, and the input array length is fixed at 5.
Algorithm A seems the clearest to me.  An improvement might be to use Vec::with_capacity to avoid the allocation.  Indeed, arrays in Rust are currently somewhat cumbersome to use.
My advice would be to wrap it in a function and not worry about it later on:
pub fn to_bytes(input: &amp;[u32]) -&gt; Vec&lt;u8&gt; {
    let mut bytes = Vec::with_capacity(4 * input.len());

    for value in input {
        bytes.extend(&amp;value.to_be_bytes());
    }

    bytes
}

#[cfg(test)]
mod tests {
    #[test]
    fn to_bytes() {
        use super::*;

        assert_eq!(to_bytes(&amp;[]), &amp;[]);

        let input = &amp;[0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0];
        let output = &amp;[
            0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89, 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x32,
            0x54, 0x76, 0xC3, 0xD2, 0xE1, 0xF0,
        ];

        assert_eq!(to_bytes(input), output);
    }
}

(playground)