Serialization and validation
Quick answer
Serialization converts structured values into a transferable representation; deserialization reconstructs values from that representation. Parsing and decoding alone do not prove that the data satisfies application rules.
Separate format checks from business rules
Define the allowed representation, bound input sizes, and validate ranges and required relationships. For example, a syntactically valid integer may represent an impossible age. For richer formats such as JSON, use a real parser rather than hand-written splitting. The first example encodes one fixed-width number; the second validates a simple integer field, not a complete serialization format.
fn main() { let bytes = 258u16.to_le_bytes(); println!("{bytes:?}"); println!("{}", u16::from_le_bytes(bytes)); }Expected stdout (not executed):
[2, 1]258The byte order is explicit and independent of host endianness.
fn quantity(text: &str) -> Result<u16, &'static str> { if text.len() > 5 { return Err("too long"); } let n: u16 = text.parse().map_err(|_| "invalid number")?; if !(1..=100).contains(&n) { return Err("out of range"); } Ok(n) }fn main() { println!("{:?}", quantity("12")); println!("{:?}", quantity("101")); }Expected stdout (not executed):
Ok(12)Err("out of range")Both strings are numeric, but only one satisfies the domain rule.
Common mistake
Do not equate a successful deserialization with safe or authorized data.
Try it yourself
Add a limit of 50 to the quantity rule and predict the result for 51.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Serialization and validation — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A decoder successfully parses an age of 500. What else must happen?
Serialization and validation — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let bytes = 258u16.to_le_bytes(); println!("{bytes:?}"); println!("{}", u16::from_le_bytes(bytes)); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Serialization and validation — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn quantity(text: &str) -> Result<u16, &'static str> { if text.len() > 5 { return Err("too long"); } let n: u16 = text.parse().map_err(|_| "invalid number")?; if !(1..=100).contains(&n) { return Err("out of range"); } Ok(n) } fn main() { println!("{:?}", quantity("12")); println!("{:?}", quantity("101")); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.