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

  • Rust Programming By Example
  • Guillaume Gomez Antoni Boucher
  • 119字
  • 2021-07-02 19:12:58

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.

主站蜘蛛池模板: 阿勒泰市| 同德县| 河源市| 宁化县| 秭归县| 怀宁县| 安阳市| 社旗县| 师宗县| 纳雍县| 蓬安县| 汤原县| 棋牌| 黎平县| 宁陵县| 台东市| 汕头市| 荃湾区| 象州县| 麻阳| 永修县| 平山县| 海盐县| 大石桥市| 喜德县| 海伦市| 启东市| 抚州市| 隆安县| 喀什市| 西峡县| 洛扎县| 务川| 抚远县| 寻乌县| 增城市| 静安区| 敦化市| 福贡县| 万山特区| 延长县|