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

Array length is not bigger than a negative number #16605

Closed
asterite opened this issue Aug 19, 2014 · 4 comments
Closed

Array length is not bigger than a negative number #16605

asterite opened this issue Aug 19, 2014 · 4 comments

Comments

@asterite
Copy link

fn main() {
  assert!([1i].len() > -1);
}

This assertion incorrectly fails.

@asterite
Copy link
Author

Forgot this:

$ rustc --version
rustc 0.12.0-pre (3570095e3 2014-08-19 10:20:54 +0000)

@killercup
Copy link
Member

I'm quite certain this is because array length is an unsigned integer and your -1 is cast to an unsigned int as well. Basically, this is a duplicate of #5477.

fn main() {
    let a = [1i];
    let mut b = a.len();
    println!("{}", b);
    b = -1;
    println!("{}", b);
}

prints

1
18446744073709551615

@emberian
Copy link
Member

Closing as dup of #5477. This isn't a bug, but can be surprising.

@asterite
Copy link
Author

And it will be, approximately two or three times a month in your forums. Good luck :-)

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

3 participants