You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-23 23:12:52 +02:00
Publish Comprehensive Rust 🦀
This commit is contained in:
17
src/unsafe/unions.md
Normal file
17
src/unsafe/unions.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Unions
|
||||
|
||||
Unions are like enums, but you need to track the active field yourself:
|
||||
|
||||
```rust,editable
|
||||
#[repr(C)]
|
||||
union MyUnion {
|
||||
i: u8,
|
||||
b: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let u = MyUnion { i: 42 };
|
||||
println!("int: {}", unsafe { u.i });
|
||||
println!("bool: {}", unsafe { u.b }); // Undefined behavior!
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user