Rust Playground Exploration with mdbook

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn about the Rust Playground, a web interface that allows you to experiment with Rust code. You will also discover how to use it with mdbook to make code examples playable and editable. Additionally, you will explore how to use it with docs and enable the "Run" button by using the html_playground_url attribute in Rust code.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") subgraph Lab Skills rust/function_syntax -.-> lab-99290{{"`Rust Playground Exploration with mdbook`"}} rust/expressions_statements -.-> lab-99290{{"`Rust Playground Exploration with mdbook`"}} end

Playground

The Rust Playground is a way to experiment with Rust code through a web interface.

Using it with mdbook

In mdbook, you can make code examples playable and editable.

fn main() {
    println!("Hello World!");
}

This allows the reader to both run your code sample, but also modify and tweak it. The key here is the adding the word editable to your codefence block separated by a comma.

```rust
//...place your code here
```

Additionally, you can add ignore if you want mdbook to skip your code when it builds and tests.

```rust
//...place your code here
```

Using it with docs

You may have noticed in some of the official Rust docs a button that says "Run", which opens the code sample up in a new tab in Rust Playground. This feature is enabled if you use the #[doc] attribute called html_playground_url.

Summary

Congratulations! You have completed the Playground lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like