]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/transmute_bytes_to_str.txt
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / src / docs / transmute_bytes_to_str.txt
1 ### What it does
2 Checks for transmutes from a `&[u8]` to a `&str`.
3
4 ### Why is this bad?
5 Not every byte slice is a valid UTF-8 string.
6
7 ### Known problems
8 - [`from_utf8`] 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 UTF-8,
11 use [`from_utf8_unchecked`] which is as fast as `transmute`
12 but has a semantically meaningful name.
13 - You might want to handle errors returned from [`from_utf8`] instead of calling `unwrap`.
14
15 [`from_utf8`]: https://doc.rust-lang.org/std/str/fn.from_utf8.html
16 [`from_utf8_unchecked`]: https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html
17
18 ### Example
19 ```
20 let b: &[u8] = &[1_u8, 2_u8];
21 unsafe {
22     let _: &str = std::mem::transmute(b); // where b: &[u8]
23 }
24
25 // should be:
26 let _ = std::str::from_utf8(b).unwrap();
27 ```