Write your first Rust Program today without installing anything

The first time you write Rust code you might be tempted to go find the complete list of everything you can install, from the compiler to Cargo the package manager, then go on to set up your editor and so on.

We’re going to skip all of that and write our first Rust program using the Rust Playground.

The Rust Playground allows us to write code on the website and then build and run our program, among other actions like formatting our code.

Your First Rust Program

When building a Rust program we have two choices, we can build a binary or a library.

A binary is something we can use to run our program, like a CLI tool or an API server.

A library, by contrast, is something we can use as a dependency of someone else’s program. For example, an argument parsing library for our CLI tool, or a routing library for an API server.

We’ll start with a binary that prints "hello, world!", which is something of a classic starting point when learning a new language.

fn main() {
println!("hello, world!");

We start off by using fn to define a function called main. For binaries, the function called main is required and special. It is the entrypoint into our program that will run whenever someone runs our binary.

Inside of our main function, we use the println! macro to print hello, world! out to the console.

We don’t have a console here, so the output is captured by Rust Playground and shown to us on the website.

Running your first Rust Program

Click “Run” to compile and run the program.

The output should look like this, with two sections. One section is labelled Standard Error and the other is labelled Standard Output.

------Standard Error------
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 4.13s
Running `target/debug/playground`
------Standard Output------
hello, world!

The reason we have two labels here is kind of esoteric so we’ll keep it high-level for now.

Standard Error is where additional information from programs gets output, while Standard Output is where the “real” output from our program goes.

The additional information in Standard Error often includes errors or warnings that may have happened during our program’s execution, but can also include information that isn’t part of our program’s output, such as the output we see above.

Standard Error: The Rust Compiler Output

The Standard Error output we see in the Rust Playground example is the output the Rust compiler gives us when compiling our program. This output includes the name of our binary (playground), the version of our binary (0.0.1) and the directory that we’re compiling our project from on the first line.

The name and version of our binary is usually defined in Cargo.toml, which Rust Playground is generating for us behind the scenes. So that’s why they’re both named “playground”.

The second line shows us which profile was used for compilation and how long the compilation took.

Cargo has two major profiles: dev and release.

The dev profile is set up to make development easier and faster. It includes debug info and does fewer optimizations to our code, saving compilation time.

The release profile is the opposite, it removes debugging information and does more optimization passes on our code. This can result in faster execution times, lower memory usage, and longer compilation times.

Finally the third line tells us which binary is being run. The target folder is where all of our build artifacts end up, the debug folder holds all of the artifacts for the dev profile builds (similarly, the release folder would hold the release profile builds), and the name of our binary is playground.

Standard Output: Our Program’s Output

The text in the Standard Output heading is our program’s output. We told our program to print hello, world! on its own line, and that’s exactly what happened.

Let’s change the output.

The println! macro accepts formatting arguments in the first string we pass to it. In this case we can replace the word world with two curly braces. This is what we’ll come to know later as the Display formatter, because it takes whatever argument we pass in and uses the Display trait to serialize it to a string that can be printed.

In this case, we’ll use our own name as the argument. As you might imagine, the Display trait implementation for a string is... a string. So there’s nothing magical going on here.

fn main() {
println!("hello, {}!", "Chris");

Run the program and you’ll see hello, <your-name> in the Standard Output.

That was your first working Rust program, congrats!

You can continue using Rust Playground as long as you’d like. There are quite a few other features, such as compiling in release mode, and you’ll get feedback for warnings and compilation errors as you go.

It even supports the usage of some (but not all) third party crates.

You could also take the next step and install Rust on your system locally.