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

  • Learning Rust
  • Paul Johnson Vesa Kaihlavirta
  • 165字
  • 2021-07-02 23:07:18

Arrays

Arrays are simple to construct. For example:

let my_array = ["Merseybus", "Amberline", "Crosville", "Liverbus", "Liverline", "Fareway"]; 

Arrays must comply with a number of rules, which are as follows:

  • The array has a fixed size. It can never grow as it is stored as a continuous memory block.
  • The contents of the array can only ever be of one type.

As with any type of variable, by default arrays are non-mutable. Even if the array is mutable, the overall size cannot be changed. For example, if an array has five elements, it cannot be changed to six.

We can also create an array with a type, as follows:

let mut my_array_two: [i32; 4] = [1, 11, 111, 1111]; 
let mut empty_array: [&str; 0] = []; 

It is also possible to create an array a number of times with the same value, as follows:

let number = [111; 5]; 

This will create an array called number with 5 elements, all initialized to a value of 111.

主站蜘蛛池模板: 安塞县| 遵义市| 富川| 东兴市| 山西省| 渝中区| 新密市| 南开区| 雷波县| 盐山县| 鹤壁市| 肃宁县| 上饶市| 凤山市| 宁武县| 长沙市| 察雅县| 阿尔山市| 玉溪市| 绥宁县| 筠连县| 通渭县| 海晏县| 海淀区| 灯塔市| 中牟县| 固始县| 瑞丽市| 南部县| 河北省| 芷江| 礼泉县| 新龙县| 崇明县| 山阴县| 鹤壁市| 大同市| 贡嘎县| 莱芜市| 遂川县| 通道|