Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconsider as-casting #199

Open
illicitonion opened this issue Oct 31, 2024 · 0 comments
Open

Reconsider as-casting #199

illicitonion opened this issue Oct 31, 2024 · 0 comments

Comments

@illicitonion
Copy link
Contributor

Very early on in the course, we introduce as casting: https://rust-exercises.com/100-exercises/02_basic_calculator/10_as_casting

A few thoughts on this:

  1. I'd argue in general converting between different sizes of integers is a much less core part of the language than the early placement of this section suggests - we may want to move it later (where we can also rely on more knowledge such as of traits). I don't think it's required for any of the exercises done before we could reasonably introduce Into/From and friends.
  2. I think we should be default recommending Into/From and TryInto/TryFrom instead of as. In general I think as should only be used where it's strictly required, and if there's a more specific conversion mechanism we should prefer that - we shouldn't train in reaching for as as a default.
  3. When we're talking about how as can cause bits to be re-interpreted, I'd recommend we throw in a link to a source on bit representation. I've had novices who only know JS/Python before (so have never encountered signed vs unsigned types) be (reasonably) confused about how to predict/interpret -1_i8 as u8 and 255_u8 as i8. We talk about how truncation keeps bytes, but without knowing what that byte representation is (i.e. what two's complement is) it can be hard to interpret. I think this can be handled with a simple one-sentence explanation and a link.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant