expression
Binary message version envelope
A version prefix selects the intended schema before decoding. Reject unsupported versions and trailing data; the prefix alone is not authentication or a transport framing protocol. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): (4, 9) version trailing Common mistake: Changing field order without changing or negotiating the binary schema version.
Formal Syntax & Structure
rust syntaxrust
version byte + typed payloadWorking Example
Example Snippetrust
fn main() { fn decode(bytes: &[u8]) -> Result<(u8, u8), &'static str> { let (&version, body) = bytes.split_first().ok_or("empty")?; if version != 1 { return Err("version"); } let (value, rest) = postcard::take_from_bytes::<(u8, u8)>(body).map_err(|_| "payload")?; if !rest.is_empty() { return Err("trailing"); } Ok(value) } println!("{:?}", decode(&[1, 4, 9]).unwrap()); println!("{}", decode(&[2, 4, 9]).unwrap_err()); println!("{}", decode(&[1, 4, 9, 0]).unwrap_err());}