File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -1100,6 +1100,24 @@ extern "rust-intrinsic" {
11001100 /// Below are common applications of `transmute` which can be replaced with safer
11011101 /// constructs.
11021102 ///
1103+ /// Turning raw bytes(`&[u8]`) to `u32`, `f64`, etc.:
1104+ ///
1105+ /// ```
1106+ /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1107+ ///
1108+ /// let num = unsafe {
1109+ /// std::mem::transmute::<[u8; 4], u32>(raw_bytes);
1110+ /// };
1111+ ///
1112+ /// // use `u32::from_ne_bytes` instead
1113+ /// let num = u32::from_ne_bytes(raw_bytes);
1114+ /// // or use `u32::from_le_bytes` or `u32::from_ge_bytes` to specify the endianness
1115+ /// let num = u32::from_le_bytes(raw_bytes);
1116+ /// assert_eq!(num, 0x12345678);
1117+ /// let num = u32::from_be_bytes(raw_bytes);
1118+ /// assert_eq!(num, 0x78563412);
1119+ /// ```
1120+ ///
11031121 /// Turning a pointer into a `usize`:
11041122 ///
11051123 /// ```
You can’t perform that action at this time.
0 commit comments