diff --git a/guide/src/format/configuration/general.md b/guide/src/format/configuration/general.md
index 20396fac..1c3884fb 100644
--- a/guide/src/format/configuration/general.md
+++ b/guide/src/format/configuration/general.md
@@ -63,8 +63,8 @@ Options for the Rust language, relevant to running tests and playground
integration.
- **edition**: Rust edition to use by default for the code snippets. Default
- is "2015". Individual code blocks can be controlled with the `edition2015`
- or `edition2018` annotations, such as:
+ is "2015". Individual code blocks can be controlled with the `edition2015`,
+ `edition2018` or `edition2021` annotations, such as:
~~~text
```rust,edition2015
diff --git a/src/book/mod.rs b/src/book/mod.rs
index 286234bd..7f1159b4 100644
--- a/src/book/mod.rs
+++ b/src/book/mod.rs
@@ -274,6 +274,10 @@ impl MDBook {
RustEdition::E2018 => {
cmd.args(&["--edition", "2018"]);
}
+ RustEdition::E2021 => {
+ cmd.args(&["--edition", "2021"])
+ .args(&["-Z", "unstable-options"]);
+ }
}
}
diff --git a/src/config.rs b/src/config.rs
index c384192c..bf4aabbb 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -467,6 +467,9 @@ pub struct RustConfig {
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
/// Rust edition to use for the code.
pub enum RustEdition {
+ /// The 2021 edition of Rust
+ #[serde(rename = "2021")]
+ E2021,
/// The 2018 edition of Rust
#[serde(rename = "2018")]
E2018,
@@ -855,6 +858,26 @@ mod tests {
assert_eq!(got.rust, rust_should_be);
}
+ #[test]
+ fn edition_2021() {
+ let src = r#"
+ [book]
+ title = "mdBook Documentation"
+ description = "Create book from markdown files. Like Gitbook but implemented in Rust"
+ authors = ["Mathieu David"]
+ src = "./source"
+ [rust]
+ edition = "2021"
+ "#;
+
+ let rust_should_be = RustConfig {
+ edition: Some(RustEdition::E2021),
+ };
+
+ let got = Config::from_str(src).unwrap();
+ assert_eq!(got.rust, rust_should_be);
+ }
+
#[test]
fn load_arbitrary_output_type() {
#[derive(Debug, Deserialize, PartialEq)]
diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index 80b06bf8..02b7da46 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -836,13 +836,15 @@ fn add_playground_pre(
{
let contains_e2015 = classes.contains("edition2015");
let contains_e2018 = classes.contains("edition2018");
- let edition_class = if contains_e2015 || contains_e2018 {
+ let contains_e2021 = classes.contains("edition2021");
+ let edition_class = if contains_e2015 || contains_e2018 || contains_e2021 {
// the user forced edition, we should not overwrite it
""
} else {
match edition {
Some(RustEdition::E2015) => " edition2015",
Some(RustEdition::E2018) => " edition2018",
+ Some(RustEdition::E2021) => " edition2021",
None => "",
}
};
@@ -1064,4 +1066,28 @@ mod tests {
assert_eq!(&*got, *should_be);
}
}
+ #[test]
+ fn add_playground_edition2021() {
+ let inputs = [
+ ("x()
",
+ "
\n#![allow(unused)]\nfn main() {\nx()\n}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ];
+ for (src, should_be) in &inputs {
+ let got = add_playground_pre(
+ src,
+ &Playground {
+ editable: true,
+ ..Playground::default()
+ },
+ Some(RustEdition::E2021),
+ );
+ assert_eq!(&*got, *should_be);
+ }
+ }
}
diff --git a/src/theme/book.js b/src/theme/book.js
index 5e386369..83295de9 100644
--- a/src/theme/book.js
+++ b/src/theme/book.js
@@ -108,9 +108,12 @@ function playground_text(playground) {
let text = playground_text(code_block);
let classes = code_block.querySelector('code').classList;
- let has_2018 = classes.contains("edition2018");
- let edition = has_2018 ? "2018" : "2015";
-
+ let edition = "2015";
+ if(classes.contains("edition2018")) {
+ edition = "2018";
+ } else if(classes.contains("edition2021")) {
+ edition = "2021";
+ }
var params = {
version: "stable",
optimize: "0",