expression

Protocol Buffers: field numbers and wire tags

The binary tag combines a field number with a three-bit wire type. The field number identifies a member within its message definition. These two examples compute small valid tags; they do not parse arbitrary input. Expected stdout (not executed): field 1: tag 0x08 field 2: tag 0x12 Common mistake: Using field names or list positions as binary field identifiers.

Formal Syntax & Structure

rust syntaxrust
(field_number << 3) | wire_type

Working Example

Example Snippetrust
fn main() {    for (number, wire_type) in [(1_u32, 0_u32), (2, 2)] {        let tag = (number << 3) | wire_type;        println!("field {number}: tag {tag:#04x}");        assert_eq!(tag >> 3, number);        assert_eq!(tag & 7, wire_type);    }}
Rust Reference