官术网_书友最值得收藏!

Tuples

Tuples differ from arrays in the way that elements of an array have to be of the same type, while items in a tuple can be a mix of types. They are heterogeneous collections and are useful for storing distinct types together. They can also be used when returning multiple values from a function. Consider the following code that uses tuples:

// tuples.rs

fn main() {
let num_and_str: (u8, &str) = (40, "Have a good day!");
println!("{:?}", num_and_str);
let (num, string) = num_and_str;
println!("From tuple: Number: {}, String: {}", num, string);
}

In the preceding code, num_and_str is a tuple of two items, (u8, &str). We can also extract values from an already declared tuple into individual variables. After printing the tuple, we destructure it on the next line into the  num and string variables, and their types are inferred automatically. That's pretty neat.

主站蜘蛛池模板: 陇川县| 阿坝| 镇远县| 益阳市| 原阳县| 井冈山市| 利津县| 明光市| 朔州市| 元谋县| 珠海市| 兴化市| 报价| 安丘市| 邯郸市| 三穗县| 启东市| 呼和浩特市| 永登县| 阳新县| 前郭尔| 临湘市| 长子县| 上栗县| 曲松县| 亳州市| 施甸县| 麦盖提县| 伽师县| 若羌县| 桃园市| 张家口市| 五莲县| 梧州市| 怀集县| 旌德县| 黑龙江省| 女性| 洛扎县| 昌乐县| 临猗县|