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

Tuples

Tuples and structures are similar, except that tuples' fields are unnamed. Tuples are declared inside parentheses, with the element separated by a comma:

let tuple = (24, 42);
println!("({}, {})", tuple.0, tuple.1);

As you can see on the second line, we can access the elements of a tuple with .index, where index is a constant and this index starts at 0.

Tuples can be used to return multiple values from a function. For instance, the str::split_at() method returns two strings:

let (hello, world) = "helloworld".split_at(5);
println!("{}, {}!", hello, world);

Here, we assign the two elements of the tuple to the hello and world variables. We'll see why this works in the Pattern matching section.

主站蜘蛛池模板: 卢龙县| 葫芦岛市| 昌图县| 民县| 波密县| 苏尼特左旗| 通州区| 张家界市| 璧山县| 达拉特旗| 徐水县| 闻喜县| 万安县| 镇平县| 潼南县| 汤阴县| 南阳市| 诸暨市| 肇东市| 云霄县| 乌兰浩特市| 方山县| 勃利县| 富川| 肃南| 曲水县| 鄄城县| 辽宁省| 张家川| 尼木县| 芦山县| 新邵县| 津南区| 普兰县| 沽源县| 东台市| 玉林市| 革吉县| 清河县| 蚌埠市| 美姑县|