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

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.

主站蜘蛛池模板: 灵璧县| 上栗县| 原平市| 青州市| 清原| 温宿县| 门头沟区| 治多县| 神农架林区| 金堂县| 纳雍县| 伽师县| 星子县| 丹巴县| 望江县| 紫金县| 巧家县| 鹿泉市| 加查县| 肃宁县| 旅游| 东乡县| 日喀则市| 扬州市| 廊坊市| 三原县| 佛教| 安庆市| 兰溪市| 桃园市| 白银市| 会宁县| 新安县| 满城县| 八宿县| 江孜县| 青浦区| 富宁县| 正镶白旗| 黔西| 天等县|