4 ms·
Still, I would rather not have Rust's 'as' cast. I should like to see all, or at least most uses of 'as' deprecated in a later Rust edition. Rust's 'as' will s
by tialaramex 4y ago
Still, I would rather not have Rust's 'as' cast. I should like to see all, or at least most uses of 'as' deprecated in a later Rust edition.
Rust's 'as' will silently throw away data to achieve what you asked. Sometimes you wanted that, sometimes you didn't realise, and requiring into() and try_into() instead helps fix that.
For example suppose I have a variable named beans, I forgot what type it is, but it's got the count of beans in it, there should definitely be a non-negative value because we were counting beans, and it shouldn't be more than a few thousand, so we're going to put that in a u16 variable named enough. let enough = beans as u16;
This works just fine, even though beans is i64 (ie a signed 64-bit integer). Until one day beans is -1 due to an arithmetic error elsewhere, and now enough is 65535, which is pretty surprising. It's not undefined but it is surprising.
If we instead write let enough = beans.into(); we get a compiler error, the compiler can't see any way to turn i64 into u16 without risk of loss, so this won't work. We can write beans.try_into().unwrap() and get a panic if actually beans was out of range, or we can actually write the try_into() handler properly if we realise, seeing it can fail, what we're actually dealing with here.
- bestouff 4y agoI feel you. I'd really like a way for "as" to be transformed into "try_into().unwrap()", say in a later edition. Or even just "into()" which would throw a nice compile-time error - This is the Rust Way.
- jcranmer 4y agoIf I can get away with using .into() over as, I will. However, there are so many cases where you can't, and .try_into().unwrap() is just way too unwieldy. In particular, there's no way for me to go "I know I'm only ever going to run on 64-bit machines where sizeof(usize) == sizeof(u64), so usize should implement From<u64>" (and even more annoying, I might be carefully making sure this works on 32-bit and 64-bit machines and get screwed because Rust thinks it could be portable to a 16-bit usize despite the fact I'm allocating hundreds of MB of memory). And of course there are times I do want to bitcast negative values to large positive unsigned values or vice versa, without error. So while I do understand why maybe you shouldn't use as, at the end of the day, it just ends up being easier to use it than not use it.
- gspr 4y ago> try_into().unwrap() is just way too unwieldy. I always carry with me a tiu() alias method for exactly that reason (in a TryIntoUnwrap trait implemented for every pair of types which implements TryInto).
- arcticbull 4y agoYou can also do try_from()? if you impl From<TryIntError> on your local error type.
- ridiculous_fish 4y agoA flip side is that some safe conversions also produce compiler errors. On a 64-bit system, usize and u64 are the same width, but they are not convertible: neither is Into the other, so you cannot lean on the compiler in the way you describe. You might use try_into(), but then you risk panicking on a 32-bit system, instead of getting a compile-time error.