Exercise
The Rust playground is convenient for testing small programs, trying new crates and libraries, and sharing code with others. In this exercise, we'll build a small program in the playground to become familiar with the environment.
Write code in the playground
Let's start by writing some code for a basic program.
Connect to the Rust playground.
Enter the following code in the playground editor:
fn main(){println!(Welcome to Rust!);}
Select Tools > Rustfmt to format the code:
The tool adjusts the code to follow official Rust styles:
Select Tools > Clippy to check for mistakes in the code. The results are displayed under the editor:
To fix the sample code, add quote marks around the text "Welcome to Rust!":
Build and run code in the playground
Now we'll compile the code and run the program.
To choose how to build and run the code in the playground, open the Run dropdown menu at the top of the UI:
Select Run to build and execute the sample program. The program output is displayed under the editor:
Save and share code in the playground
As you work in the playground, the code is automatically saved to the browser storage. If you close the browser window, you can lose the code that you entered. To make the code always available, you can create a shareable URL.
Select the Share feature on the toolbar to create a GitHub gist for the code in the playground:
Select the paper icon next to the text Permalink to the playground to get a shareable gist for the code.
Now you can save the URL to access the code later, or share the URL for others to see the code.