-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Duration.as_nanos() as u64
exceeds u64::MAX
#137814
Comments
Fails also on both:
and,
|
This seems expected to me. The code essentially minimizes to: 18446744073709551615000000000_u128 as u64 That is |
Spelling out the answer to the issue title because this confused me too:
these numbers happen to look extremely similar in decimal and the first one ends in ...1616 while Indeed this modified assertion passes (and is flagged by clippy as being tautological): use std::time::Duration;
fn main() {
let duration = Duration::from_secs(u64::MAX);
assert!(duration.as_nanos() as u64 <= u64::MAX);
} |
What's happening is somewhat clearer if you print the hex and align:
|
Thanks! This all makes sense and I had indeed missed that the Is that just because of the |
Yes, casting from a larger integer to a smaller one with |
Thank you, I've definitely had this misconception about how EDIT: and this rfc: rust-lang/rfcs#2484 (comment) |
I tried this code:
I expected to see this happen: The assert to pass.
Instead, this happened: The assert fails with the following:
Does not seem related to
u128
, as this passes:Meta
rustc --version --verbose
:Backtrace
I doub't the backtrace is much use, but just in case.
The text was updated successfully, but these errors were encountered: