expression

Protocol Buffers: mutually exclusive oneof alternatives

A oneof expresses alternatives in a message. An Option wrapping a Rust enum models either one selected variant or no selection. A receiver must still validate business rules such as a nonempty SKU. Expected stdout (not executed): id 7 sku N-7 missing lookup Common mistake: Assuming oneof guarantees one alternative is present or semantically valid.

Formal Syntax & Structure

rust syntaxrust
oneof lookup { uint64 id = 1; string sku = 2; }

Working Example

Example Snippetrust
fn main() {    enum Lookup { Id(u64), Sku(String) }    fn describe(value: Option<Lookup>) -> String {        match value {            Some(Lookup::Id(id)) => format!("id {id}"),            Some(Lookup::Sku(sku)) => format!("sku {sku}"),            None => "missing lookup".into(),        }    }    for value in [Some(Lookup::Id(7)), Some(Lookup::Sku("N-7".into())), None] {        println!("{}", describe(value));    }}
Rust Reference