forked from rust-lang/spec
-
Notifications
You must be signed in to change notification settings - Fork 0
Array slice sample #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JoelMarcey
wants to merge
9
commits into
main
Choose a base branch
from
array-slice-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46b6e2e
Add array section
JoelMarcey 5f76e25
Fix examples
JoelMarcey 9c2ba03
Update specification/arrays.md
JoelMarcey 953f44e
Update specification/arrays.md
JoelMarcey 8c23d2d
Update specification/arrays.md
JoelMarcey 62d6f22
Update specification/arrays.md
JoelMarcey 7895dbe
Update specification/arrays.md
JoelMarcey d878382
Update specification/arrays.md
JoelMarcey 85790f2
Update specification/arrays.md
JoelMarcey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
An array is a numbered, fixed-size sequence of elements of a single [type](link to type spec), called the element type. The element type of an array can be any Rust [type](link to type spec). | ||
|
||
The size of an array must evaluate to a non-negative constant expression represented by at value of type `usize`. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Arrays are stored in contiguous memory. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Arrays can be mutable, insofar as their elements can change, but the size and type are always immutable. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Array Type Specification | ||
|
||
``` | ||
array-type: | ||
: '[' any-type ; array-size ']' '=' array-initializer | ||
; | ||
|
||
any-type: | ||
: [type](link to type spec) | ||
; | ||
|
||
array-size: | ||
: ['usize'](link to usize spec) | ||
; | ||
|
||
array-initializer: | ||
: '[' array-element-list ']' | ||
| '[' *repeat-expression ']' | ||
; | ||
|
||
array-element-list*: | ||
: array-element | ||
| array-element-list ',' array-element | ||
; | ||
|
||
array-element: | ||
: expression | ||
; | ||
|
||
repeat-expression: | ||
: expression ; ['usize'](link to usize spec) | ||
; | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
let mut _spec_a: [i32; 3] = [3; 3]; // repeat expression | ||
let _spec_b: [String; 2] = [String::from("hi"), String::from("bye")]; | ||
let _spec_c: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; | ||
let _spec_d: [u64; 0] = []; | ||
``` | ||
|
||
|
||
## Array Creation | ||
|
||
There are two ways to create an array, either via a list of specific elements of the same type or a repeat expression of the same type. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` | ||
array-initializer: | ||
: '[' array-element-list ']' | ||
| '[' *repeat-expression ']' | ||
; | ||
|
||
array-element-list*: | ||
: array-element | ||
| array-element-list ',' array-element | ||
; | ||
|
||
array-element: | ||
: expression | ||
; | ||
|
||
repeat-expression: | ||
: expression ; ['usize'](link to usize spec) | ||
; | ||
``` | ||
|
||
### Examples | ||
|
||
#### List | ||
|
||
``` | ||
let _cre_a: [i32; 2] = [3, 3]; | ||
let _cre_b: [String; 2] = [String::from("hi"), String::from("bye")]; | ||
``` | ||
|
||
#### Repeat Expression | ||
|
||
``` | ||
let _cre_c = [0; 3]; | ||
let _cre_d = ["repeat"; 10]; | ||
``` | ||
|
||
## Array Access | ||
|
||
Arrays are accessed by index, with the first element of the array having an index of 0. Thus, the range of indices for an array in Rust is `0..(array-size - 1)`. | ||
|
||
Any attempt to access an array out side the bounds of its size will result in an `index out of bounds` compiler error. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` | ||
array-access-expression: | ||
: array '[' array-index ']' | ||
; | ||
|
||
array-index: | ||
: usize | ||
; | ||
``` | ||
|
||
## Array Updates | ||
|
||
If the array is mutable, as defined by the `mut` keyword, the array elements can be updated. The size and type of the array can never change. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Updating the value of an array element is performed by specifying the index of the element to be changed and setting it to the new value. | ||
JoelMarcey marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we want assignment like this to automatically invoke an |
||
|
||
``` | ||
array-update-expression: | ||
: array-access-expression '=' expression | ||
; | ||
|
||
array-access-expression: | ||
: array '[' array-index ']' | ||
; | ||
|
||
array-index: | ||
: usize | ||
; | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
let mut upd_a: [i32; 3] = [1, 2, 4]; | ||
upd_a[2] = 3; | ||
|
||
let mut upd_b: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; | ||
upd_b[0] = [0, 0]; | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the size of the element type of an array need to be known at compile time? I am not sure that this is even applicable, but I thought that I would note it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cf https://spec.ferrocene.dev/types-and-traits.html#syntax_arraytypespecification
where it says that the element type needs to have a fixed size.