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

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.

主站蜘蛛池模板: 郯城县| 诸暨市| 通州市| 固原市| 武安市| 怀来县| 嘉定区| 胶南市| 永州市| 铜山县| 富裕县| 龙州县| 崇州市| 渑池县| 武穴市| 巴林右旗| 汾阳市| 长顺县| 射阳县| 江油市| 鄂托克旗| 云阳县| 那曲县| 遵化市| 张家界市| 汽车| 民乐县| 盱眙县| 宁化县| 漾濞| 石狮市| 鄄城县| 石屏县| 阜平县| 清水河县| 呼玛县| 翁牛特旗| 清苑县| 民乐县| 金塔县| 泸定县|