Description
- I have searched open and closed issues and pull requests for duplicates, using these search terms:
- 2021
- ch9 (found this ch9.2 : outdated error quoted #1262 )
- ch09
- I have checked the latest
main
branch to see if this has already been fixed, in this file:
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#recoverable-errors-with-result
https://doc.rust-lang.org/nightly/book/ch09-02-recoverable-errors-with-result.html#recoverable-errors-with-result
Description of the problem:
There's an example there, this one:
use std::fs::File;
fn main() {
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {error:?}"),
};
}
which when you press the Play button, it clearly uses one of 2015 or 2018 editions instead of the 2021 one because the errors are these:
Compiling playground v0.0.1 (/playground)
warning: unused variable: `greeting_file`
--> src/main.rs:6:9
|
6 | let greeting_file = match greeting_file_result {
| ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_greeting_file`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `error`
--> src/main.rs:8:13
|
8 | Err(error) => panic!("Problem opening the file: {error:?}"),
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_error`
warning: panic message contains an unused formatting placeholder
--> src/main.rs:8:57
|
8 | Err(error) => panic!("Problem opening the file: {error:?}"),
| ^^^^^^^^^
|
= note: this message is not used as a format string when given without arguments, but will be in Rust 2021
= note: `#[warn(non_fmt_panics)]` on by default
help: add the missing argument
|
8 | Err(error) => panic!("Problem opening the file: {error:?}", ...),
| +++++
help: or add a "{}" format string to use the message literally
|
8 | Err(error) => panic!("{}", "Problem opening the file: {error:?}"),
| +++++
warning: `playground` (bin "playground") generated 3 warnings (run `cargo fix --bin "playground"` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.74s
Running `target/debug/playground`
thread 'main' panicked at src/main.rs:8:23:
Problem opening the file: {error:?}
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Also note that there's no button to open that code block in playground, there's only copy code and play button(which shows the output inline, while viewing the book), so to try it in playground, one must manually open the already-known playground url then paste the copied code there and see that it actually works (due to 2021 edition being default in playground).
Here's 2018 edition on playground
Here's 2021 edition on playground
The latter works as expected.
Suggested fix:
I don't know where this edition is enforced for playground(it doesn't seem like this knows), but, at least for this example, it should be edition=2021
, because 2015 and 2018 show the above warnings and as you can see, it doesn't display correct output Problem opening the file: {error:?}
which should be Problem opening the file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
.
I've checked the linked file and it is in a project with edition 2021, however, that doesn't mean that the play button knows to use that edition for playground.
It would seem that the workaround1 is quite possibly adding edition2021
here just like it's done in
book/src/ch04-02-references-and-borrowing.md
Line 173 in ee5c7ec
A built book.js
has 2015 as the default edition:
let text = playground_text(code_block);
let classes = code_block.querySelector('code').classList;
let edition = "2015";
if(classes.contains("edition2018")) {
edition = "2018";
} else if(classes.contains("edition2021")) {
edition = "2021";
}
well this default is from mdBook and been there since 2021 or earlier.
Footnotes
-
it's just a workaround because who knows in how many other places this is needed; ideally, that edition value would be gotten from
Cargo.toml
instead, somehow, everywhere where there's a play button... ↩