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

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.

主站蜘蛛池模板: 登封市| 文成县| 灵宝市| 射洪县| 台山市| 武穴市| 潮安县| 隆安县| 郓城县| 开鲁县| 精河县| 九龙坡区| 汶川县| 广饶县| 伊宁县| 图们市| 新余市| 麦盖提县| 扶沟县| 班玛县| 临沂市| 仁化县| 伊吾县| 宣城市| 洛川县| 左权县| 黎川县| 海口市| 洛隆县| 门源| 通辽市| 大足县| 延寿县| 顺昌县| 友谊县| 莆田市| 郓城县| 灵璧县| 芜湖县| 大丰市| 阿鲁科尔沁旗|