- 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.
推薦閱讀
- Objective-C應(yīng)用開發(fā)全程實(shí)錄
- CMDB分步構(gòu)建指南
- 算法零基礎(chǔ)一本通(Python版)
- Java面向?qū)ο笏枷肱c程序設(shè)計(jì)
- 零基礎(chǔ)玩轉(zhuǎn)區(qū)塊鏈
- 神經(jīng)網(wǎng)絡(luò)編程實(shí)戰(zhàn):Java語言實(shí)現(xiàn)(原書第2版)
- Mastering Unity Shaders and Effects
- 零基礎(chǔ)學(xué)單片機(jī)C語言程序設(shè)計(jì)
- Go語言精進(jìn)之路:從新手到高手的編程思想、方法和技巧(2)
- Sails.js Essentials
- 軟技能2:軟件開發(fā)者職業(yè)生涯指南
- Building Microservices with Go
- Java Web 從入門到項(xiàng)目實(shí)踐(超值版)
- 生成藝術(shù):Processing視覺創(chuàng)意入門
- Hands-On Data Visualization with Bokeh