小编Zar*_*hyo的帖子

元组实现`复制'吗?

在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次坐标.

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

tuples rust

5
推荐指数
1
解决办法
472
查看次数

标签 统计

rust ×1

tuples ×1