Convert u64 to i64

I found some stack overflows for u64 -> usize but nothing for u64 -> i64 besides this crate maybe:

This seems to work:

let num: u64 = 17;

num as i64;
1 Like

For unconditional, possibly overflowing conversion, you can use as, as in the_u64_value as i64. For gracefully handling errors, as opposed to ignoring them, you can use TryFrom or TryInto, e.g. i64::try_from(the_u64_value).

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.