Playground
Rust Playground 是一种通过网页界面体验 Rust 代码的方式。
在 mdbook
中使用
在 mdbook
中,你可以使代码示例可运行和可编辑。
fn main() { println!("Hello World!"); }
这允许读者既可以运行你的代码示例,也可以修改和调整它。这里的关键是在你的代码块分隔符中添加用逗号分隔的 editable
单词。
```rust,editable
//...place your code here
```
此外,如果你希望 mdbook
在构建和测试时跳过你的代码,你可以添加 ignore
。
```rust,editable,ignore
//...place your code here
```
在文档中使用
你可能已经在一些 官方 Rust 文档 中注意到一个名为“Run”的按钮,点击它会在 Rust Playground 的新标签页中打开代码示例。如果你使用名为 html_playground_url
的 #[doc]
属性,则会启用此功能。
#![doc(html_playground_url = "https://play.rust-lang.org/")]
//! ```
//! println!("Hello World");
//! ```