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

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.

主站蜘蛛池模板: 固原市| 天等县| 渭源县| 建始县| 邢台县| 鹤峰县| 岑巩县| 靖安县| 三门峡市| 和硕县| 濮阳市| 伊宁县| 东方市| 阿荣旗| 新源县| 紫金县| 搜索| 桃园市| 新建县| 江阴市| 克什克腾旗| 盖州市| 建始县| 泰兴市| 临沧市| 忻城县| 凤城市| 长岭县| 讷河市| 杭锦后旗| 镶黄旗| 容城县| 阿勒泰市| 屏边| 六枝特区| 远安县| 德清县| 容城县| 巨鹿县| 桃源县| 桐庐县|