]> git.lizzy.rs Git - rust.git/blob - src/docs/transmute_int_to_char.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / transmute_int_to_char.txt
1 ### What it does
2 Checks for transmutes from an integer to a `char`.
3
4 ### Why is this bad?
5 Not every integer is a Unicode scalar value.
6
7 ### Known problems
8 - [`from_u32`] which this lint suggests using is slower than `transmute`
9 as it needs to validate the input.
10 If you are certain that the input is always a valid Unicode scalar value,
11 use [`from_u32_unchecked`] which is as fast as `transmute`
12 but has a semantically meaningful name.
13 - You might want to handle `None` returned from [`from_u32`] instead of calling `unwrap`.
14
15 [`from_u32`]: https://doc.rust-lang.org/std/char/fn.from_u32.html
16 [`from_u32_unchecked`]: https://doc.rust-lang.org/std/char/fn.from_u32_unchecked.html
17
18 ### Example
19 ```
20 let x = 1_u32;
21 unsafe {
22     let _: char = std::mem::transmute(x); // where x: u32
23 }
24
25 // should be:
26 let _ = std::char::from_u32(x).unwrap();
27 ```