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

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.

主站蜘蛛池模板: 开原市| 长泰县| 西藏| 丘北县| 余庆县| 凤台县| 兰西县| 平安县| 铁岭县| 邯郸市| 宾川县| 莱阳市| 介休市| 广汉市| 漯河市| 新巴尔虎右旗| 宜城市| 沽源县| 兰考县| 曲靖市| 辽阳市| 湘潭县| 涿州市| 朔州市| 安远县| 广安市| 延吉市| 都匀市| 大英县| 迁安市| 镇赉县| 嘉善县| 临安市| 罗田县| 永修县| 获嘉县| 民丰县| 融水| 自治县| 庆城县| 精河县|