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

Add generic range function to the prelude #5361

Closed
brson opened this issue Mar 13, 2013 · 4 comments
Closed

Add generic range function to the prelude #5361

brson opened this issue Mar 13, 2013 · 4 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@brson
Copy link
Contributor

brson commented Mar 13, 2013

uint::range is one of the most common ways to iterate, but the uint:: prefix is a bit unsightly. Add a generic range function, parametrized over whatever traits are appropriate and add it to the prelude. Then convert all uses of range.

@brson
Copy link
Contributor Author

brson commented Mar 13, 2013

Integer inference could make this strategy backfire. One most often wants to iterate over uints but range(0, 10) will infer ints. Maybe inference will pick up the correct type from the closure body but I'm not sure.

@sophiebits
Copy link
Contributor

If you want uints inside the loop, it doesn't seem so strange to have to write range(0u, 10u).

@thestinger
Copy link
Contributor

@brson: what would you think about a generic trait for enumerated types with a to method? So something like for 5i.to(&10) |x| { ... } or for "foo".to(&"bar") { ... }.

Here's Haskell's approach to this:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Enum

I think #6023 would be a reasonable approach to the integer inference issue (inferring, but not falling back to int).

@thestinger
Copy link
Contributor

Implemented as an iterator by #8216.

bors added a commit to rust-lang-ci/rust that referenced this issue May 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants