rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow

admin2025-04-30  0

I am just starting out learning Rust using the book "Hands-on Rust: Effective Learning through 2D Game Development and Play" by Herbert Wolverson.

I'm currently at Part I.3 and am tasked with building a simple game using bracket-lib. I should be able to run a simple "Hello, world" in the book, but trying to run cargo run results in the following error:

thread 'main' panicked at core/src/panicking.rs:221:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed 'isize::MAX'
note: run with 'RUST_BACKTRACE=1' environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
Aborted (core dumped)

I rechecked every line but was not able to find any issues with it. VSCode with rust-analyzer also does not show any issues.

I uploaded the code here:

Maybe someone could point me in the right direction. I appreciate any help provided.

OS used: Fedora 41 (fully patched)
Rustup Ver: rustup 1.27.1 (54dd3d00f 2024-04-24)
rustc Ver: rustc 1.83.0 (90b35a623 2024-11-26)

I am just starting out learning Rust using the book "Hands-on Rust: Effective Learning through 2D Game Development and Play" by Herbert Wolverson.

I'm currently at Part I.3 and am tasked with building a simple game using bracket-lib. I should be able to run a simple "Hello, world" in the book, but trying to run cargo run results in the following error:

thread 'main' panicked at core/src/panicking.rs:221:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed 'isize::MAX'
note: run with 'RUST_BACKTRACE=1' environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
Aborted (core dumped)

I rechecked every line but was not able to find any issues with it. VSCode with rust-analyzer also does not show any issues.

I uploaded the code here: https://github.com/flashkiller999/learning_rust

Maybe someone could point me in the right direction. I appreciate any help provided.

OS used: Fedora 41 (fully patched)
Rustup Ver: rustup 1.27.1 (54dd3d00f 2024-04-24)
rustc Ver: rustc 1.83.0 (90b35a623 2024-11-26)

Share Improve this question edited Jan 6 at 10:43 Jmb 23.7k2 gold badges37 silver badges68 bronze badges asked Jan 5 at 1:01 Void4Void4 111 silver badge1 bronze badge 4
  • You have a bug in your dependency. – Chayim Friedman Commented Jan 5 at 1:13
  • In general, please put the relevant code in the question instead of an external repo (a minimal reproducible example). That said, looks like a known issue: github.com/amethyst/bracket-lib/issues/362 – Ry- Commented Jan 5 at 1:19
  • It works just fine on Windows 11 64-bit. Start the debugging with running a simple Hello World app, there must be something wrong with your rustc and its targets setup. – Raeisi Commented Jan 5 at 13:17
  • Same as #362?! – user2722968 Commented Jan 5 at 14:52
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks for the answers, the problem is upstream in bracket-lib, it seems to be broken on Wayland environments at the moment as mentioned in the link from Ry- & user2722968 (https://github.com/amethyst/bracket-lib/issues/362)

I was able to workaround the issue with the workaround mention in the bug report, via using X11 instead of Wayland: WINIT_UNIX_BACKEND=x11 cargo run

For using the workaround in VSCode I edited the launch.json and added the environment variable to it:

"env": {
    "WINIT_UNIX_BACKEND": "x11"
}
转载请注明原文地址:http://anycun.com/QandA/1746022952a91477.html