mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-18 11:26:27 +00:00
Improve Array & Tuple notes
This commit is contained in:
parent
e9425c19a4
commit
6980fd786b
1 changed files with 14 additions and 3 deletions
17
Rust/Rust.md
17
Rust/Rust.md
|
@ -174,10 +174,11 @@ Tuples have a *fixed length*: once declared, they cannot grow or shrink in size.
|
||||||
|
|
||||||
```rs
|
```rs
|
||||||
let tup: (i32, f64, u8) = (500, 6.4, 1);
|
let tup: (i32, f64, u8) = (500, 6.4, 1);
|
||||||
|
let tup = (500, 6.4, 1);
|
||||||
|
|
||||||
let (x, y, z) = tup; // tuple deconstruction (unpacking)
|
let (x, y, z) = tup; // tuple deconstruction (unpacking)
|
||||||
|
|
||||||
tup.0; // member access
|
tup.0 = value; // member access & update (mut be mutable)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Array Types
|
### Array Types
|
||||||
|
@ -188,9 +189,19 @@ An array isn't as flexible as the `vector` type, though. A vector is a similar c
|
||||||
```rs
|
```rs
|
||||||
let array = [0, 1, 2, 3, 4];
|
let array = [0, 1, 2, 3, 4];
|
||||||
let array: [Type; length] = [...];
|
let array: [Type; length] = [...];
|
||||||
let array: [value; length]; // same as python's [value] * length
|
let array: [value; length]; // repeat expression (same as python's [value] * length)
|
||||||
|
|
||||||
array[index] = value; // member access and update
|
let index: usize = <index_value>; // indexes and ranges must be of type usize
|
||||||
|
array[index] = value; // member access and update (must be mutable)
|
||||||
|
|
||||||
|
let matrix = [
|
||||||
|
[0 ,1, 2],
|
||||||
|
[3, 4, 5]
|
||||||
|
]
|
||||||
|
|
||||||
|
let matrix: [[Type, length]; length]; = [[...], [...], ...]
|
||||||
|
|
||||||
|
matrix[row][column];
|
||||||
```
|
```
|
||||||
|
|
||||||
### Slice Types
|
### Slice Types
|
||||||
|
|
Loading…
Add table
Reference in a new issue