1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-22 15:57:46 +02:00

Add speaker notes for unions.

This commit is contained in:
Andrew Walbran 2023-01-17 16:35:54 +00:00
parent d7fcf8216a
commit 47e28e87c0

View File

@ -15,3 +15,14 @@ fn main() {
println!("bool: {}", unsafe { u.b }); // Undefined behavior!
}
```
<details>
Unions are very rarely needed in Rust as you can usually use an enum. They are occasionally needed
for interacting with C library APIs.
If you just want to reinterpret bytes as a different type, you probably want
[`std::mem::transmute`](https://doc.rust-lang.org/stable/std/mem/fn.transmute.html) or a safe
wrapper such as the [`zerocopy`](https://crates.io/crates/zerocopy) crate.
</details>