expression

Protocol Buffers: explicit scalar presence

An explicitly optional scalar distinguishes absence from a provided default value. Option<u32> models that distinction in Rust. This is a local representation, not a claim about a particular generator. An implicit-presence scalar cannot generally express that distinction. Expected stdout (not executed): unknown stock stock: 0 stock: 5 Common mistake: Treating missing stock as proof of zero units.

Formal Syntax & Structure

rust syntaxrust
optional uint32 stock = 3;

Working Example

Example Snippetrust
fn main() {    fn describe(stock: Option<u32>) -> String {        match stock {            None => "unknown stock".into(),            Some(n) => format!("stock: {n}"),        }    }    for stock in [None, Some(0), Some(5)] {        println!("{}", describe(stock));    }}
Rust Reference