Skip to content

feat(stdlib): add subarray method for fixed-size arrays#11560

Open
illegalcall wants to merge 7 commits intonoir-lang:masterfrom
illegalcall:feat-stdlib-subarray
Open

feat(stdlib): add subarray method for fixed-size arrays#11560
illegalcall wants to merge 7 commits intonoir-lang:masterfrom
illegalcall:feat-stdlib-subarray

Conversation

@illegalcall
Copy link
Copy Markdown
Contributor

Description

Problem

Resolves #11106

Users frequently need to extract a fixed-size subarray from a larger array. Currently this requires manually writing a helper function with a loop, as noted by multiple users in the issue and the related #1240.

Summary

Adds a subarray method to [T; N] in the stdlib that returns a contiguous slice of M elements starting at a given offset.

let arr = [10, 20, 30, 40, 50];
let sub: [Field; 3] = arr.subarray(1);
assert_eq(sub, [20, 30, 40]);

Implementation details:

  • Uses crate::mem::zeroed() for initialization, consistent with concat, map, and mapi — avoids requiring a T: Empty trait bound.
  • Includes a bounds assertion (offset + M <= N) with a descriptive error message, following the stdlib convention used in vector::as_array, poseidon2::absorb, etc.
  • Adds 3 unit tests: middle extraction, full array copy, and zero-length output.
  • Adds documentation entry in docs/docs/noir/concepts/data_types/arrays.md.

Additional Context

The original issue proposed a standalone function with a T: Empty bound. This PR instead implements it as a method on [T; N] using mem::zeroed(), which is more ergonomic and consistent with existing array methods.

User Documentation

Check one:

  • No user documentation needed.
  • Changes in docs/ included in this PR.
  • [For Experimental Features] Changes in docs/ to be submitted in a separate PR.

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution to the Noir language.

Please do not force push to this branch after the Noir team have started review of this PR. Doing so will only delay us merging your PR as we will need to start the review process from scratch.

Thanks for your understanding.

Copy link
Copy Markdown
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after pending suggestions. This has come up often enough where I do think we should add this to the stdlib. IMO, the name subarray is good in that it hints the operation is more expensive than a traditional slice operation which returns an offset reference.

Comment thread docs/docs/noir/concepts/data_types/arrays.md
Comment thread docs/docs/noir/concepts/data_types/arrays.md
Comment thread noir_stdlib/src/array/mod.nr
@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

@illegalcall do you mind running cargo t --release and accepting snapshot changes via cargo insta accept and committing that? I can't push to this branch otherwise I'd run them myself. This should fix the failing Test / Run tests (partition N) CI instances.

@illegalcall
Copy link
Copy Markdown
Contributor Author

Hey @jfecher, absolutely my pleasure to run the scripts. I have accepted the changes.

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

@illegalcall sorry, one more command to run for stdlib changes: just stdlib_docs or if you don't have or want to install just, then:

$ cd noir_stdlib && nargo doc
$ rm -rf noir_stdlib/docs
$ mv noir_stdlib/target/docs noir_stdlib/docs

(I'd also appreciate if you could add subarray to cspell.json)

@illegalcall
Copy link
Copy Markdown
Contributor Author

Yeah, ofcourse. Doing this right now.

@illegalcall
Copy link
Copy Markdown
Contributor Author

Done. Lets hope CI passes this time 🤞

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

@illegalcall looks like there's still a diff when running just stdlib_docs again. Can you run it again?

@illegalcall
Copy link
Copy Markdown
Contributor Author

illegalcall commented Feb 12, 2026

@jfecher Um, I am not sure what am I missing.

I have redone the cargo t --release build,
run just stdlib_docs : creates docs, no diff
run cd noir_stdlib && nargo doc : creates docs, no diff.

run

$ rm -rf noir_stdlib/docs
$ mv noir_stdlib/target/docs noir_stdlib/docs

still no diff.

Should i rollback 1 commit and regen and force push?
Do you have any other ideas i can try to actually be sure about the issue.

To be extra sure, i have cloned the repo again and ran the same commands, only to reach the same outcome. :(

(I am eager to know about the issue, and would love to work with you and fix this. illegalcall is my discord username too, and i am on Noir server)

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

@illegalcall odd, when I pulled this PR and ran just stdlib_docs locally I saw a diff. You can try installing just and running the command that way but it should just be a shortcut for the commands I gave.

@illegalcall
Copy link
Copy Markdown
Contributor Author

@jfecher Yeah, when i run i dont see a diff.
image

@illegalcall
Copy link
Copy Markdown
Contributor Author

And following the docs rm and mv commands results in the same doc.
@jfecher If youre able to see the diff, can you paste the diff here and i apply the diff?

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

@illegalcall it calls nargo doc directly so maybe you need a cargo b beforehand or ensure the nargo it is referring to is your local one and not a global one you have installed elsewhere. After that, do you see any uncommitted changes?

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

The diff is here https://github.com/noir-lang/noir/actions/runs/21960924441/job/63438631636?pr=11560 but I've also copied it down below:

+++ b/noir_stdlib/docs/nargo_doc.js
@@ -30,6 +30,11 @@ function showSidebar() {
 }
 
 function hideSidebar() {
+  // Only hide the sidebar if we are on a mobile device
+  if (document.documentElement.clientWidth >= 1200) {
+    return;
+  }
+
   main.style.display = 'block';
   sidebar.style.display = 'none';
   sidebarShown = false;
diff --git a/noir_stdlib/docs/styles.css b/noir_stdlib/docs/styles.css
index f526dd2318..8a7d737f21 100644
--- a/noir_stdlib/docs/styles.css
+++ b/noir_stdlib/docs/styles.css
@@ -25,6 +25,8 @@
     --string-color: light-dark(#718c00, #83a300);
     --doccomment-color: light-dark(#4d4d4c, #8ca375);
     --interpolation-color: light-dark(#119850, #bcca7d);
+    --table-border-color: #e0e0e0;
+    --table-alt-row-background-color: light-dark(#f5f5f5, #2a2a2a);
 
     --sidebar-toggle-height: 82px;
 }
@@ -137,6 +139,10 @@ ul.item-list, ul.sidebar-list {
     font-family: "Source Serif 4", NanumBarunGothic, serif;
 }
 
+.comments pre {
+    overflow: auto;
+}
+
 .comments code, .item-description code {
     background-color: var(--code-color);
     border-radius: 6px;
@@ -305,6 +311,20 @@ ul.item-list, ul.sidebar-list {
     overflow-x: hidden;
 }
 
+table {
+    margin: .5em 0;
+    border-collapse: collapse;
+}
+
+table th, table td {
+    padding: .5em;
+    border: 1px solid var(--table-border-color);
+}
+
+table tbody tr:nth-child(2n) {
+    background: var(--table-alt-row-background-color);
+}
+
 /* mobile */
 @media (max-width: 1199px) {
   body {

@illegalcall
Copy link
Copy Markdown
Contributor Author

illegalcall commented Feb 12, 2026

Yes @jfecher, i didnt have to use the diff starting with cargo b and reinstalling nargo helped.
Hopefully it passes now :)

@jfecher jfecher enabled auto-merge February 12, 2026 21:49
@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

CC @TomAFrench may need a force merge here as the upload brillig execution reports CI step is failing.

@jfecher
Copy link
Copy Markdown
Contributor

jfecher commented Feb 12, 2026

Thanks for all the changes @illegalcall! PR looks good now.

@illegalcall
Copy link
Copy Markdown
Contributor Author

Glad to be a part of this, and thank you @jfecher for helping out.

@illegalcall
Copy link
Copy Markdown
Contributor Author

Its unfortunate to see so many merge conflicts here 🙁. Should i go ahead and resolve them, if there is a posiblity of merging this soon? @jfecher

@TomAFrench
Copy link
Copy Markdown
Member

I'll pull this branch into the repo so we can clean up the merge conflicts and get it over the line

@illegalcall
Copy link
Copy Markdown
Contributor Author

Thank you so much, lmk if anything comes up with which I can help. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: Consider adding subarray method to array in stdlib

3 participants