Tutorial by Examples

Rust tuples, as in most other languages, are fixed-size lists whose elements can all be of different types. // Tuples in Rust are comma-separated values or types enclosed in parentheses. let _ = ("hello", 42, true); // The type of a tuple value is a type tuple with the same number of el...
Rust programs use pattern matching extensively to deconstruct values, whether using match, if let, or deconstructing let patterns. Tuples can be deconstructed as you might expect using match fn foo(x: (&str, isize, bool)) { match x { (_, 42, _) => println!("it's 42"),...
To access elements of a tuple directly, you can use the format .n to access the n-th element let x = ("hello", 42, true); assert_eq!(x.0, "hello"); assert_eq!(x.1, 42); assert_eq!(x.2, true); You can also partially move out of a tuple let x = (String::from("hello&quo...
A tuple is simply a concatenation of multiple values: of possibly different types whose number and types is known statically For example, (1, "Hello") is a 2 elements tuple composed of a i32 and a &str, and its type is denoted as (i32, &'static str) in a similar fashion as i...
// It's possible to unpack tuples to assign their inner values to variables let tup = (0, 1, 2); // Unpack the tuple into variables a, b, and c let (a, b, c) = tup; assert_eq!(a, 0); assert_eq!(b, 1); // This works for nested data structures and other complex data types let complex = ((1,...

Page 1 of 1