let prefix = u16::from(network);
let mut bytes = match prefix {
0..=63 => vec![prefix as u8],
64..=16_383 => {
let first = ((prefix & 0b0000_0000_1111_1100) as u8) >> 2;
let second = ((prefix >> 8) as u8) | ((prefix & 0b0000_0000_0000_0011) as u8) << 6;
vec![first | 0b01000000, second]
},
_ => Err(Error::UnsupportedNetwork(network.into()))?,
};
u16 has to_be_bytes and from_be_bytes methods (and little endian versions) that you can use if you aren't doing anything other than splitting it into bytes
OP is definitely doing a lot more than that. From what I can understand, the conversion is lossy, since if prefix >= 64, then the bottom 2 bits are removed from first. Then, the bottom two bits are slapped onto the 8th and 9th bit (counting from 0) with OR, and the whole thing is shifted down by 8 positions, creating second.