Do `Copy` tuples adjust?

In the Rust Book, chapter 18, they give an example of a tuple versus a pattern.

fn print_coordinates(&(x, y): &(i32, i32)) {
    println!("Current location: ({}, {})", x, y);
}

fn main() {
    let point = (3, 5);
    print_coordinates(&point);   // point passed as reference
}

Out of curiosity, I tried without going as a link like this.

fn print_coordinates((x, y): (i32, i32)) {
    println!("Current location: ({}, {})", x, y);
}

fn main() {
    let point = (3, 5);
    print_coordinates(point);   // point passed as value
    print_coordinates(point);   // point is still valid here
}

It compiles and prints coordinates 2 times.

Can tuples be passed to functions in the same way as other primitive data types (numbers, Booleans, etc.)?

+4
source share
1 answer

Yes; according to docs , this is true for arity tuples 12 or less:

If each type inside a tuple implements one of the following features, then the tuple itself also implements it.

Due to a time limit in a system like Rust, these traits are only implemented on tuples arity 12 or less. This may change in the future.

+6

Source: https://habr.com/ru/post/1684321/


All Articles