rust-lang / rust-lang/rust-by-example
Unexpected results from unsafe Type casting
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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