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

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.

主站蜘蛛池模板: 长丰县| 岐山县| 榆社县| 康乐县| 江城| 峨山| 阿瓦提县| 曲沃县| 汾西县| 都江堰市| 齐齐哈尔市| 汝州市| 洛川县| 桃园市| 永兴县| 石家庄市| 宜昌市| 繁峙县| 高阳县| 屯昌县| 马龙县| 深泽县| 枣庄市| 乐安县| 贡嘎县| 光山县| 太仆寺旗| 合水县| 葵青区| 库车县| 剑阁县| 江油市| 鹰潭市| 和田市| 同德县| 旅游| 乐昌市| 天水市| 苍南县| 安顺市| 烟台市|