在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
}
出于好奇,我试着不经过这样的参考。
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
}
它编译并打印出2次坐标。
可以像其他原始数据类型(数字,布尔值等)一样将元组传递给函数吗?