rust-lang / rust-lang/rust-by-example

Unexpected results from unsafe Type casting

Open
#1,666 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Handlebars
Stars
8.1k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

Under type/cast.md, the example below:
unsafe { // -100.0 as u8 is 156 println!("-100.0 as u8 is : {}", (-100.0_f32).to_int_unchecked::<u8>()); }
returns 0 as undefined behavior rather than 156 as expected result.
Please update the documentation accordingly so that it doesn't confuse the reader who compiles this code on their local machine.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open type/cast.md and inspect the unsafe cast example for (-100.0_f32).to_int_unchecked::(). Update the documentation so it accurately describes the undefined behavior and does not promise 156 when readers compile and run the example locally.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.