8.6 Tuples as Parameters and Return Types

Tuples group together multiple values of possibly different types.

8.6.1 Passing Tuples to Functions

fn print_point(point: (i32, i32)) {
    println!("Point is at ({}, {})", point.0, point.1);
}
fn main() {
    let p = (10, 20);
    print_point(p);
}

8.6.2 Returning Tuples from Functions

fn swap(x: i32, y: i32) -> (i32, i32) {
    (y, x)
}
fn main() {
    let a = 5;
    let b = 10;
    let (b, a) = swap(a, b);
    println!("a: {}, b: {}", a, b);
}
  • The swap function returns a tuple containing the swapped values.