Skip to content

Tracking Issue i64 to u64 (as) #140870

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

Closed
AveNRU opened this issue May 9, 2025 · 5 comments
Closed

Tracking Issue i64 to u64 (as) #140870

AveNRU opened this issue May 9, 2025 · 5 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@AveNRU
Copy link

AveNRU commented May 9, 2025

let x: i64 = -10;
let y: u64 = x as u64;
println!("{}", y); // 18446744073709551606

Please help with error when do not worry transform i64 to u64

@AveNRU AveNRU added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC S-tracking-unimplemented Status: The feature has not been implemented. labels May 9, 2025
@zachs18
Copy link
Contributor

zachs18 commented May 9, 2025

The as cast operator does bitcasts for integer types, and the 64-bit 2's complement representation of -10 is 0b1111111111111111111111111111111111111111111111111111111111110110, so as casting it to u64 gives the u64 with that same bit pattern (18446744073709551606).

If you want to keep the same number value, and get an error if it is not representable, you could use TryFrom/TryInto instead of as-casting, e.g.

let x: i64 = -10;
let y: Result<u64, _> = x.try_into(); // or u64::try_from(x)
println!("{:?}", y); // Err(TryFromIntError(()))
let x: i64 = 10;
let y: u64 = x.try_into().unwrap();
println!("{:?}", y); // 10

(Tracking issues are generally for tracking the implementation of things like new features, not for bug reports (which should use one of the other issue templates) or debugging questions (which should generally go on the user forum))

@rustbot label -C-tracking-issue -S-tracking-unimplemented +C-discussion

@rustbot rustbot added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC S-tracking-unimplemented Status: The feature has not been implemented. labels May 9, 2025
@AveNRU
Copy link
Author

AveNRU commented May 9, 2025

Sorry for tags

@ChrisDenton
Copy link
Member

I would suggest asking rust questions on one of the community pages https://www.rust-lang.org/community or other sites like stackoverflow.

@AveNRU
Copy link
Author

AveNRU commented May 9, 2025

https://www.collabora.com/news-and-blog/blog/2025/05/06/matt-godbolt-sold-me-on-rust-by-showing-me-c-plus-plus/
This question was discussed by Matt Godbolt above when comparing c++ and rust. Is it possible to fully protect via as

@ShE3py
Copy link
Contributor

ShE3py commented May 10, 2025

Clippy has lints that detect as conversions:

You can enable them by promoting them to warnings/errors:
https://doc.rust-lang.org/rustc/lints/levels.html

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants