include_markdown!() { /* proc-macro */ }Expand description
Include code from within a code fence in a markdown file.
Two arguments are required: a file path relative to the current source file, and a name defined within the code fence as shown below.
All CommonMark code fences are supported.
§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(())
}