元组实现`复制'吗?

Zar*_*hyo 5 tuples rust

在Rust Book,第18章中,他们举了一个模式匹配元组的例子.

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
}
Run Code Online (Sandbox Code Playgroud)

出于好奇,我尝试了没有作为这样的参考传递.

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
}
Run Code Online (Sandbox Code Playgroud)

它编译并打印出2次坐标.

元组是否可以像其他原始数据类型(数字,布尔值等)一样传递给函数?

Sam*_*tep 8

是; 根据文档,这对于12或更少的arity元组来说是正确的:

如果元组中的每个类型都实现了以下特征之一,那么元组本身也会实现它.

由于Rust类型系统的临时限制,这些特征仅在12或更小的元组上实现.将来,这可能会改变.