Take a look:
use std::convert::{From, TryFrom};
fn main() {
let size: usize = 42;
let good: u128 = u128::try_from(size).unwrap(); // works fine
let bad: u128 = u128::from(size); // doesn't compile!
}
As far as I know, usize
is an integral type and those are never larger than 128 bits. Therefore, I see no way the usize -> u128
conversion can fail. So, why doesn't u128
implement From<usize>
?
Update: Rust's documentation says:
From T for U implies Into U for T
Though usize -> u128
seems fine, u128 -> usize
doesn't. OK, but why isn't Into<u128>
implemented for usize
instead?