expression

Read and write binary integers with explicit byte order

Specify big-endian or little-endian wire order independently of machine byte order. Fixed-width fields still need complete input and cancellation handling. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): [12, 34, 34, 12] 4660 4660 Common mistake: Decoding the same field with a different endianness from the sender.

Formal Syntax & Structure

rust syntaxrust
write_u16; read_u16; write_u16_le; read_u16_le

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::io::{AsyncReadExt, AsyncWriteExt};    let mut bytes = Vec::new();    bytes.write_u16(0x1234).await.unwrap();    bytes.write_u16_le(0x1234).await.unwrap();    println!("{:02x?}", bytes);    let mut r = bytes.as_slice();    println!("{} {}", r.read_u16().await.unwrap(), r.read_u16_le().await.unwrap());}
Rust Reference