diff --git a/cspell.json b/cspell.json index 1174a56dd33..c1cc19f8962 100644 --- a/cspell.json +++ b/cspell.json @@ -60,6 +60,7 @@ "combinators", "compinit", "comptime", + "concat", "cpus", "cranelift", "critesjosh", diff --git a/docs/docs/noir/concepts/data_types/arrays.md b/docs/docs/noir/concepts/data_types/arrays.md index 289145a8c4d..f831a5f57b8 100644 --- a/docs/docs/noir/concepts/data_types/arrays.md +++ b/docs/docs/noir/concepts/data_types/arrays.md @@ -255,6 +255,23 @@ fn main() { } ``` +### concat + +Concatenates this array with another array. + +```rust +fn concat(self, array2: [T; M]) -> [T; N + M] +``` + +```rust +fn main() { + let arr1 = [1, 2, 3, 4]; + let arr2 = [6, 7, 8, 9, 10, 11]; + let concatenated_arr = arr1.concat(arr2); + assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]); +} +``` + ### as_str_unchecked Converts a byte array of type `[u8; N]` to a string. Note that this performs no UTF-8 validation - diff --git a/noir_stdlib/src/array/mod.nr b/noir_stdlib/src/array/mod.nr index 85cc0580aae..7a74410a673 100644 --- a/noir_stdlib/src/array/mod.nr +++ b/noir_stdlib/src/array/mod.nr @@ -136,6 +136,29 @@ impl [T; N] { } ret } + + /// Concatenates this array with another array. + /// + /// Example: + /// + /// ```noir + /// fn main() { + /// let arr1 = [1, 2, 3, 4]; + /// let arr2 = [6, 7, 8, 9, 10, 11]; + /// let concatenated_arr = arr1.concat(arr2); + /// assert(concatenated_arr == [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]); + /// } + /// ``` + pub fn concat(self, array2: [T; M]) -> [T; N + M] { + let mut result = [self[0]; N + M]; + for i in 1..N { + result[i] = self[i]; + } + for i in 0..M { + result[i + N] = array2[i]; + } + result + } } impl [T; N] @@ -232,4 +255,12 @@ mod test { fn map_empty() { assert_eq([].map(|x| x + 1), []); } + + #[test] + fn concat() { + let arr1 = [1, 2, 3, 4]; + let arr2 = [6, 7, 8, 9, 10, 11]; + let concatenated_arr = arr1.concat(arr2); + assert_eq(concatenated_arr, [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]); + } }