expression

Postcard fixed output buffer

Serialize into a caller-provided buffer and use only the returned slice. A buffer that is too small produces an error rather than growing automatically. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 1 42 true Common mistake: Sending the whole buffer instead of the returned initialized encoded range.

Formal Syntax & Structure

rust syntaxrust
postcard::to_slice(&value, &mut buffer)

Working Example

Example Snippetrust
fn main() {    let mut buf = [0u8; 8];    let used = postcard::to_slice(&42u8, &mut buf).unwrap();    println!("{} {}", used.len(), used[0]);    let mut empty = [];    println!("{}", postcard::to_slice(&42u8, &mut empty).is_err());}
Rust Reference