3 ms·
What’s wrong with .try_into::<usize>().unwrap()?
by pwdisswordfish9 5y ago
What’s wrong with .try_into::<usize>().unwrap()?
- ekhjarta 5y agoIt fails at runtime and not at compile time and it only fails if the value is out of range, so you have to test with large enough values to trigger the assertion during testing. We want to be able to index with integer types that are smaller than or has the same size as usize, but not with integer types that are larger than usize. And we want those checks at compile time.
- dgb23 5y agoUnwrap isn’t an evil thing to avoid completely. The type system cannot understand all invariants, but you can, and you can make sure they are correctly implemented.
- deleted 5y ago[deleted]
- masklinn 5y ago.into() will work for u8 and u16, as rust assumes >= 16b pointers.
- est31 5y agoWhich totally makes sense. u8 means 256 bytes of addressable memory. You might design chips with such limited memory, but maybe high level (read non assembly) languages are not the right tool for targetting them.
- edflsafoiewq 5y agoIt's hideous.