I want the following
(2u128.pow(x)) as u64
but such that it succeeds for x < 64 and fails for x >= 64.
I want the following
(2u128.pow(x)) as u64
but such that it succeeds for x < 64 and fails for x >= 64.
As you correctly pointed out yourself, you should use TryFrom, but you should also make sure the exponentiation itself doesn't overflow, by using u128::checked_pow instead of u128::pow:
use std::convert::TryFrom;
let x = 129;
let y = 2u128.checked_pow(x).expect("Number too big for exponentiation");
let z = u64::try_from(y).expect("Number too big for cast");
Okay, found the answer, there is TryFrom.
use std::convert::TryFrom;
u64::try_from(2u128.pow(x)).expect("Number too big for cast.")
from here which I somehow missed with my first search.