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

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.

主站蜘蛛池模板: 涪陵区| 齐齐哈尔市| 庆城县| 汤原县| 崇仁县| 渝北区| 东源县| 万山特区| 双鸭山市| 兰西县| 静乐县| 曲阳县| 体育| 高清| 定结县| 保亭| 红桥区| 呼伦贝尔市| 连云港市| 吴江市| 宝坻区| 罗定市| 柘荣县| 铁岭市| 左权县| 徐水县| 凤山县| 建瓯市| 罗城| 洛浦县| 汤阴县| 井陉县| 甘孜县| 房山区| 金寨县| 三台县| 延寿县| 遵义县| 河北省| 通化市| 郧西县|