#Are conversions using 'as' bad practice?

14 messages · Page 1 of 1 (latest)

plush crater
#

I remember about reading that somewhere a few days ago, claiming try_into() should always be used instead. Is it the case?

amber aurora
#

Usually it leads to more bug-proof code because it helps detect conversion errors

#

as can silently result in lossy conversions

#

Which you might not want

#

There is also .into() if the type can always losslessly convert

plush crater
#

so I guess I should prioritize those before using as

#

thanks a lot!

amber aurora
#

It would be good practice, yeah

#

Sometimes as is fine, but if you want to prioritize correctness, you should try to use Into/TryInto as often as possible

plush crater
#

alright, great

tranquil gazelle
#

FYI: If you use an as, and it may result in a lossy conversation, I believe the compiler flags it up with a warning...

#

Given, as only works for primatives as well,

amber aurora
#

The lint isn't enabled by default, I think

sour gazelle
#

it isn't