Skip to content
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

feat: Add slice append #2241

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/execution_success/slices/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ fn main(x : Field, y : pub Field) {
assert(remove_slice[3] == 3);
assert(remove_slice.len() == 4);

let append = [1, 2].append([3, 4, 5]);
assert(append.len() == 5);
assert(append[0] == 1);
assert(append[4] == 5);

regression_2083();
}

Expand Down
10 changes: 9 additions & 1 deletion noir_stdlib/src/slice.nr
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ impl<T> [T] {
/// the removed element
#[builtin(slice_remove)]
fn remove(_self: Self, _index: Field) -> (Self, T) { }
}

// Append each element of the `other` slice to the end of `self`.
// This returns a new slice and leaves both input slices unchanged.
fn append(mut self, other: Self) -> Self {
for elem in other {
self = self.push_back(elem);
}
self
}
}