primitive-type

Rust tuple: group a fixed number of differently typed values

A tuple groups values by position. Each element has its own type and the number of elements is part of the tuple type. Access elements with .0, .1, and so on, or destructure them. Expected output (not executed): Tea: 2 Why: Destructuring binds the string reference and integer to separate names. Common mistake: A one-element tuple requires a trailing comma; (5) is only a grouped integer expression.

Formal Syntax & Structure

rust syntaxrust
(T1, T2); (T,)

Working Example

Example Snippetrust
fn main() {    let order: (&str, u32) = ("Tea", 2);    let (name, quantity) = order;    println!("{name}: {quantity}");}
Rust Reference