MdBook and Rust 2024 edition

Does mdbook not support the new Rust 2024 edition yet?

Some days ago, I started porting the books's examples to Rust 2024 -- actually it is more testing with Rust 2024, as most examples should be compatible.

For that, I edited book.toml:

[rust]
edition = "2024"

and rebuild the book with "mdbook clean; mdbook build" and shipped it with rsync.

The strange thing is, that the FFI example runs fine when clicking the run button, but when I copy it into Rust's playground (which uses now Rust 2024 as default) compile fails, because extern needs now an unsafe block. If I switch playground to 2021 edition, it works.

My only explanation is currently, that mdbook masks 2024 internally still ?

Edition 2024 support was added to mdbook 8 months ago and first released in v0.4.41. I don't know what is causing the discrepancy between mdbook and the playground.

Thanks for your fast reply. I will then ask in the mdbook GitHub issue tracker, even when answers there might took a bit longer. Also I might try the "mdbook test" command, which runs all the code examples. Perhaps that will report code examples incompatible with the 2024 edition.

Actually, "mdbook test" fails to compile as desired. So I think it is at least not my own issue, but something with mdbook or the playground.

$ cd rust_for_c_programmers
$ mdbook test

....

failures:

---- ch25/25_6_calling_c_functions_ffi.md - _::_5_6_Calling_C_Functions__FFI_ (line 5) stdout ----
error: extern blocks must be unsafe
 --> ch25/25_6_calling_c_functions_ffi.md:7:1
  |
3 | / extern "C" {
4 | |     fn abs(input: i32) -> i32;
5 | | }
  | |_^

error: aborting due to 1 previous error

I just tried the run button of one of your examples and the following JSON is send as the request payload to the playground (note that the edition is set to 2015—looks like a bug in the play button; it ignoring the edition of the code snippet):

{"version":"stable","optimize":"0","code":"#[link(name = \"c\")]\nextern \"C\" {\n    fn abs(input: i32) -> i32;\n}\n\nfn main() {\n    let value = -42;\n    // Calling an external fn is unsafe because Rust cannot verify its implementation.\n    unsafe {\n        let result = abs(value);\n        println!(\"abs({}) = {}\", value, result);\n    }\n}","edition":"2015"}

Looks like the bug originates here:

Edit: your book.js looks outdated:

let edition = "2015";
if(classes.contains("edition2018")) {
    edition = "2018";
} else if(classes.contains("edition2021")) {
    edition = "2021";
}

Thank you very much for the detailed investigation. Do you suggest modifying file book.js manually? I got mdbook frm the Gentoo-Linux package manager, it is the latest version v0.4.45.

Note that Eric Huss reacted this time very fast to this issue, see Rust 2024 edition is ignored for code examples · Issue #2556 · rust-lang/mdBook · GitHub. But they closed it already. I assume the issue might get fixed in the next release.

Ha, didn't realise I was looking at the already fixed version of book.js, I hadn't checked the history. That was pretty fast indeed. The next version will contain the fix for sure, it is already merged into master. If you don't want to wait for the next release, applying the fix to your book.js manually wouldn't be too bad IMO, it's just the 5 lines of code from the PR. You probably need to exclude book.js from rsync though.

1 Like