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

Inclusive loops (e.g. 0..=length) #6171

Closed
NikolayKostadinov21 opened this issue Sep 29, 2024 · 0 comments · Fixed by #6200
Closed

Inclusive loops (e.g. 0..=length) #6171

NikolayKostadinov21 opened this issue Sep 29, 2024 · 0 comments · Fixed by #6200
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@NikolayKostadinov21
Copy link

NikolayKostadinov21 commented Sep 29, 2024

Problem

Inclusive ranges are especially useful if you want to iterate over every possible value in a range. Currently the iteration in Noir is like in Rust 1.0 through 1.25 versions. If you want inclusion of the last element, you need to add one to your end value like:

fn main() {
    for i in 0..(26 + 1) {
        println!("{}", i);
    }
}

Happy Case

In Rust >=1.26 versions, you can now create an inclusive range and it will be very beneficial to add this syntactic sugar to Noir as well.
Example:

fn main() {
    for i in 0..=26 {
        println!("{}", i);
    }
}

Project Impact

Nice-to-have

Would you like to submit a PR for this Issue?

None

@NikolayKostadinov21 NikolayKostadinov21 added the enhancement New feature or request label Sep 29, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 29, 2024
@TomAFrench TomAFrench added the good first issue Good for newcomers label Sep 30, 2024
@aakoshh aakoshh mentioned this issue Oct 2, 2024
5 tasks
github-merge-queue bot pushed a commit that referenced this issue Oct 3, 2024
# Description

## Problem\*

Resolves #6171

## Summary\*

Adds support for inclusive for syntax, e.g. `for i in 0..=10`.

## Additional Context

- Added a `ForBounds` struct to handle the desugaring of `start..=end`
into `start..end+1`.
- `ForBounds` has an `inclusive` flag; I thought about adding a
`ForRange::RangeInclusive` similar to `std::ops::RangeInclusive` in
Rust, but then I thought `for` loops will not have the other variants
like `..end`, `start..` and `..`, which wouldn't have two separate
expressions, and the flag is simpler.
- Transforming an inclusive range to Hir and back makes it
non-inclusive, but I think that's fine because the block created by
`ForRange::into_for` to iterate over the array is not turned back
either.


## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants