From 46b6e2efff38a6bff516f6fbfe13487df7e0d0c0 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 5 Jan 2024 14:21:57 -0800 Subject: [PATCH 1/9] Add array section --- specification/arrays.md | 137 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 specification/arrays.md diff --git a/specification/arrays.md b/specification/arrays.md new file mode 100644 index 0000000..2e090a8 --- /dev/null +++ b/specification/arrays.md @@ -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`. + +Arrays are stored in contiguous memory. + +Arrays can be mutable, insofar as their elements can change, but the size and type are always immutable. + +## 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 a: [i32; 3] = [3; 3]; // repeat expression +let b: [String; 2] = [String::from("hi"), String::from("bye")]; +let c: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; +let e: [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. + +``` +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 a: [i32; 2] = [3, 3]; +let b: [String; 2] = [String::from("hi"), String::from("bye")]; +``` + +#### Repeat Expression + +``` +let a = [0; 3]; +let b = [String::from("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. + +``` +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. + +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. + +``` +array-update-expression: + : array-access-expression '=' expression + ; + +array-access-expression: + : array '[' array-index ']' + ; + +array-index: + : usize + ; +``` + +### Examples + +``` +let mut int_array: [i32; 3] = [1, 2, 4]; +int_array[2] = 3; + +let mut arr_array: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; +arr_array[0] = [0, 0]; +``` From 5f76e25254a823ac8fc88c9d62256e8af2ba18f7 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Wed, 10 Jan 2024 13:50:35 -0800 Subject: [PATCH 2/9] Fix examples --- specification/arrays.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/specification/arrays.md b/specification/arrays.md index 2e090a8..ce154dd 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -43,10 +43,10 @@ repeat-expression: ### Examples ``` -let mut a: [i32; 3] = [3; 3]; // repeat expression -let b: [String; 2] = [String::from("hi"), String::from("bye")]; -let c: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; -let e: [u64; 0] = []; +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] = []; ``` @@ -79,15 +79,15 @@ repeat-expression: #### List ``` -let a: [i32; 2] = [3, 3]; -let b: [String; 2] = [String::from("hi"), String::from("bye")]; +let _cre_a: [i32; 2] = [3, 3]; +let _cre_b: [String; 2] = [String::from("hi"), String::from("bye")]; ``` #### Repeat Expression ``` -let a = [0; 3]; -let b = [String::from("repeat"), 10]; +let _cre_c = [0; 3]; +let _cre_d = ["repeat"; 10]; ``` ## Array Access @@ -129,9 +129,9 @@ array-index: ### Examples ``` -let mut int_array: [i32; 3] = [1, 2, 4]; -int_array[2] = 3; +let mut upd_a: [i32; 3] = [1, 2, 4]; +upd_a[2] = 3; -let mut arr_array: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; -arr_array[0] = [0, 0]; +let mut upd_b: [[i32; 2]; 3] = [[1, 2], [3, 4], [5, 6]]; +upd_b[0] = [0, 0]; ``` From 9c2ba03cb082366c8c9bca358a68a7be65cbc785 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:32:57 -0800 Subject: [PATCH 3/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index ce154dd..306381d 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -52,7 +52,9 @@ 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. +There are two ways to initialize an array at the time of its declaration: +1. a list of specific elements whose type matches the element type of the array being declared; or +2. a repeat expression whose expression has the same type as the element type of the array being declared. ``` array-initializer: From 953f44e1c82416c313f0063e183e2d5b6087cd8f Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:33:12 -0800 Subject: [PATCH 4/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index 306381d..ce2534d 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -96,7 +96,7 @@ let _cre_d = ["repeat"; 10]; 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. +Using an index `i` of type `usize` and value `i > (array-size - 1)` will result in an `index out of bounds` compilation error. ``` array-access-expression: From 8c23d2d6cd804115b230c7921583adcdb2d8e637 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:33:28 -0800 Subject: [PATCH 5/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index ce2534d..01f2041 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -110,7 +110,7 @@ array-index: ## 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. +If the array is declared mutable by using the `mut` keyword, the array elements can be updated. Again, the size and type of the array are immutable. 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. From 62d6f2241efd66ff030773a972f45d3724ec7486 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:33:51 -0800 Subject: [PATCH 6/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index 01f2041..cd518a0 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -2,7 +2,7 @@ An array is a numbered, fixed-size sequence of elements of a single [type](link The size of an array must evaluate to a non-negative constant expression represented by at value of type `usize`. -Arrays are stored in contiguous memory. +Array elements are stored contiguously in memory. Arrays can be mutable, insofar as their elements can change, but the size and type are always immutable. From 7895dbe132708d79126afbb7216b389c6bde5fce Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:34:17 -0800 Subject: [PATCH 7/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index cd518a0..12452b8 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -4,7 +4,7 @@ The size of an array must evaluate to a non-negative constant expression represe Array elements are stored contiguously in memory. -Arrays can be mutable, insofar as their elements can change, but the size and type are always immutable. +The size and type of an array are immutable; the elements of an array are immutable, too, unless the array is declared mutable. ## Array Type Specification From d878382fa97e590c895ffceea1009d9bb1888a5d Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:34:26 -0800 Subject: [PATCH 8/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index 12452b8..e7482a6 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -1,6 +1,6 @@ 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`. +The size of an array must evaluate to a non-negative constant expression represented by a value of type `usize`. Array elements are stored contiguously in memory. From 85790f2294887ff8a5f54cd95d9adfc1705f3553 Mon Sep 17 00:00:00 2001 From: Joel Marcey Date: Fri, 19 Jan 2024 10:35:48 -0800 Subject: [PATCH 9/9] Update specification/arrays.md Co-authored-by: Will Hawkins --- specification/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specification/arrays.md b/specification/arrays.md index e7482a6..43f5151 100644 --- a/specification/arrays.md +++ b/specification/arrays.md @@ -112,7 +112,7 @@ array-index: If the array is declared mutable by using the `mut` keyword, the array elements can be updated. Again, the size and type of the array are immutable. -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. +Updating the value of an array element is performed by specifying the index of the element to be changed and setting it to a new value whose type is the same as the type of the elements of the array. ``` array-update-expression: