include_markdown!() { /* proc-macro */ }Expand description
Include code from within a code fence in a Markdown file.
All CommonMark code fences are supported.
§Arguments
path(Required) Path relative to the crate root directory.name(Required) Name of the code fence to include.scopeInclude the snippet in braces{ .. }.relative(Requires rustc 1.88 or newer) Path is relative to the source file calling the macro.
§Examples
Consider the following code fence in a crate README.md Markdown file:
```rust example
let m = example()?;
assert_eq!(format!("{m:?}"), r#"Model { name: "example" }"#);
```In Rust documentation comments, we can use # line to hide setup code.
That’s not possible in Markdown, so we can include only the code we want to demonstrate;
however, we can still compile and even run it in Rust tests:
struct Model {
name: String,
}
fn example() -> Result<Model, Box<dyn std::error::Error>> {
Ok(Model { name: "example".into() })
}
#[test]
fn test_example() -> Result<(), Box<dyn std::error::Error>> {
include_markdown!("README.md", "example");
Ok(())
}