- 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.
推薦閱讀
- 從零開(kāi)始構(gòu)建企業(yè)級(jí)RAG系統(tǒng)
- Clojure Programming Cookbook
- Python程序設(shè)計(jì)教程(第2版)
- 造個(gè)小程序:與微信一起干件正經(jīng)事兒
- JavaScript Unlocked
- TestNG Beginner's Guide
- Hands-On JavaScript High Performance
- JavaScript+Vue+React全程實(shí)例
- MongoDB權(quán)威指南(第3版)
- 數(shù)據(jù)結(jié)構(gòu)案例教程(C/C++版)
- Orchestrating Docker
- Java Web開(kāi)發(fā)實(shí)例大全(基礎(chǔ)卷) (軟件工程師開(kāi)發(fā)大系)
- Mastering Concurrency Programming with Java 9(Second Edition)
- Java Web動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)(第2版·微課版)
- Android開(kāi)發(fā)進(jìn)階實(shí)戰(zhàn):拓展與提升