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

Unable to create a range to max value, e.g. 100..256 for a Range<u8> #23635

Closed
samdoshi opened this issue Mar 23, 2015 · 4 comments
Closed

Unable to create a range to max value, e.g. 100..256 for a Range<u8> #23635

samdoshi opened this issue Mar 23, 2015 · 4 comments

Comments

@samdoshi
Copy link

How is one supposed to create a range which includes the maximum value as it's upper bound?

E.g. if I wish to create the range 100 to 255 inclusive for a u8, I need use the notation 100..256:

fn main() {
    let r: std::ops::Range<u8> = 100..256;
    println!("{:?}", r);
}

The output of which is:

100..0

(we do get a warning when we compile though)

test.rs:2:39: 2:42 warning: literal out of range for u8, #[warn(overflowing_literals)] on by default
test.rs:2     let r: std::ops::Range<u8> = 100..256;
                                                ^~~

I understand why this is happening based on the implementation of Range, and I'm not sure how one would fix it without changing Range to use an inclusive upper bound (even if the notation stays as is). Maybe a better warning might be of use if I can't be solved?

@GuillaumeGomez
Copy link
Member

Even worse:

let r: std::ops::Range<u8> = 100..(255 + 1);

We don't have a warning here whereas it displays:

100..0

But it displays a warning in this case:

let r: std::ops::Range<u8> = 100..(257 - 1);

Kinda tricky somehow haha.

@klutzy
Copy link
Contributor

klutzy commented Mar 25, 2015

range_inclusive exists for the case, but it has been deprecated recently.
cc @alexcrichton @aturon

@aturon
Copy link
Member

aturon commented Mar 25, 2015

Note: range_inclusive isn't deprecated, just unstable.

@steveklabnik
Copy link
Member

This issue has been superceded by #28237, once that feature is stable, this will be possible.

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

No branches or pull requests

5 participants