diff --git a/guide/src/format/mdbook.md b/guide/src/format/mdbook.md
index 9bb94615ce..a354112920 100644
--- a/guide/src/format/mdbook.md
+++ b/guide/src/format/mdbook.md
@@ -4,7 +4,8 @@
There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
-For the Rust language, you can use the `#` character as a prefix which will hide lines [like you would with Rustdoc][rustdoc-hide].
+For the Rust language, you can prefix lines with `# ` (`#` followed by a space) to hide them [like you would with Rustdoc][rustdoc-hide].
+This prefix can be escaped with `##` to prevent the hiding of a line that should begin with the literal string `# ` (see [Rustdoc's docs][rustdoc-hide] for more details)
[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example
diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index d0149fb523..2150c37f63 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -931,7 +931,7 @@ fn add_playground_pre(
// we need to inject our own main
let (attrs, code) = partition_source(code);
- format!("# #![allow(unused)]\n{attrs}#fn main() {{\n{code}#}}").into()
+ format!("# #![allow(unused)]\n{attrs}# fn main() {{\n{code}# }}").into()
};
content
}
@@ -1003,12 +1003,9 @@ fn hide_lines_rust(content: &str) -> String {
result += &caps[3];
result += newline;
continue;
- } else if &caps[2] != "!" && &caps[2] != "[" {
+ } else if matches!(&caps[2], "" | " ") {
result += "";
result += &caps[1];
- if &caps[2] != " " {
- result += &caps[2];
- }
result += &caps[3];
result += newline;
result += "";
@@ -1134,7 +1131,7 @@ mod tests {
fn add_playground() {
let inputs = [
("x()
",
- "
# #![allow(unused)]\n#fn main() {\nx()\n#}
"),
+ "# #![allow(unused)]\n# fn main() {\nx()\n# }
"),
("fn main() {}
",
"fn main() {}
"),
("let s = \"foo\n # bar\n\";
",
@@ -1164,7 +1161,7 @@ mod tests {
fn add_playground_edition2015() {
let inputs = [
("x()
",
- "# #![allow(unused)]\n#fn main() {\nx()\n#}
"),
+ "# #![allow(unused)]\n# fn main() {\nx()\n# }
"),
("fn main() {}
",
"fn main() {}
"),
("fn main() {}
",
@@ -1188,7 +1185,7 @@ mod tests {
fn add_playground_edition2018() {
let inputs = [
("x()
",
- "# #![allow(unused)]\n#fn main() {\nx()\n#}
"),
+ "# #![allow(unused)]\n# fn main() {\nx()\n# }
"),
("fn main() {}
",
"fn main() {}
"),
("fn main() {}
",
@@ -1212,7 +1209,7 @@ mod tests {
fn add_playground_edition2021() {
let inputs = [
("x()
",
- "# #![allow(unused)]\n#fn main() {\nx()\n#}
"),
+ "# #![allow(unused)]\n# fn main() {\nx()\n# }
"),
("fn main() {}
",
"fn main() {}
"),
("fn main() {}
",
@@ -1237,8 +1234,12 @@ mod tests {
fn hide_lines_language_rust() {
let inputs = [
(
- "\n# #![allow(unused)]\n#fn main() {\nx()\n#}
",
+ "\n# #![allow(unused)]\n# fn main() {\nx()\n# }
",
"\n#![allow(unused)]\nfn main() {\nx()\n}
",),
+ // # must be followed by a space for a line to be hidden
+ (
+ "\n#fn main() {\nx()\n#}
",
+ "\n#fn main() {\nx()\n#}
",),
(
"fn main() {}
",
"fn main() {}
",),