Skip to content

Commit f7cefbb

Browse files
bors[bot]adamgreig
andauthored
Merge #317
317: Fix use of unaligned references in example r=eldruin a=adamgreig A recent change in Rust to deny unaligned reference by default (rust-lang/rust#95372) caused the upstream rust build of this book to break: rust-lang/rust#96108 (comment) This PR removes the unaligned references, instead using `addr_of!()` to get a raw pointer to print. Co-authored-by: Adam Greig <[email protected]>
2 parents 0d73660 + 058141f commit f7cefbb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/c-tips/index.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,13 @@ struct Foo {
357357

358358
fn main() {
359359
let v = Foo { x: 0, y: 0, z: 0 };
360-
// Unsafe is required to borrow a field of a packed struct.
361-
unsafe { println!("{:p} {:p} {:p}", &v.x, &v.y, &v.z) };
360+
// References must always be aligned, so to check the addresses of the
361+
// struct's fields, we use `std::ptr::addr_of!()` to get a raw pointer
362+
// instead of just printing `&v.x`.
363+
let px = std::ptr::addr_of!(v.x);
364+
let py = std::ptr::addr_of!(v.y);
365+
let pz = std::ptr::addr_of!(v.z);
366+
println!("{:p} {:p} {:p}", px, py, pz);
362367
}
363368

364369
// 0x7ffd33598490 0x7ffd33598492 0x7ffd33598493

0 commit comments

Comments
 (0)