Skip to content

u32 as char: suggested code is unchanged #97160

Closed
@Zoybean

Description

@Zoybean

Given the following code: playground link

pub fn u32_as_char(x: u32) -> char {
    (x as u32) as char
}

The current output is:

error[[E0604]](https://doc.rust-lang.org/stable/error-index.html#E0604): only `u8` can be cast as `char`, not `u32`
 --> src/lib.rs:3:5
  |
3 |     (x as u32) as char
  |     ^^^^^^^^^^^^^^^^^^ invalid cast
  |
help: try `char::from_u32` instead
 --> src/lib.rs:3:5
  |
3 |     (x as u32) as char
  |     ^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0604`.

Ideally the output should look like:

error[[E0604]](https://doc.rust-lang.org/stable/error-index.html#E0604): only `u8` can be cast as `char`, not `u32`
 --> src/lib.rs:3:5
  |
3 |     (x as u32) as char
  |     ^^^^^^^^^^^^^^^^^^ invalid cast
  |
help: try `char::from_u32` instead
 --> src/lib.rs:3:5
  |
3 |     char::from_u32(x as u32)
  |     ^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0604`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions