DEV Community

Cover image for REPL-like environment for Rust (A Rust Playground Locally)
Menard Maranan
Menard Maranan

Posted on • Updated on

REPL-like environment for Rust (A Rust Playground Locally)

TL;DR: I made a simple REPL-like environment program (similar to Python's REPL or interactive interpreter) for Rust. In a sense, it's a Rust Playground, but locally, right through your terminal, so you can play along some Rust code without creating a new file and compiling it.

Repo Link is here: https://github.com/menard-codes/rust_playground_locally (also, read the caution at the end of this blogpost)

Check the short demo video below to see it in action.

So I'm currently learning Rust.

As per my background, this is my first low-level language, and I'm still getting used to compiling my code. Coming from interpreted languages such as Python, I kinda miss the REPL (as it always come in handy when testing small pieces of code).

And for a language like Rust, the closest thing is to use the Rust playground.

I admit, I'm a lazy dev. It's sometimes annoying to constantly open a browser, create a new tab, and search 'rust playground' just to test a small piece of code. Comparing it to Python's REPL which is more straightforward, I can just open the terminal and do my thing there.

So I thought of making something similar for Rust, a REPL-like environment to run Rust code without the need to create a new file and compile it, all done right through the terminal.

So I just did that.

And to give you an idea, here's what it looks like:

So how did I made this?

First off, I wondered around the Rust Playground.

I tried running a simple hello world program, and opened the dev tools and checked the networks tab to see what's what. From there, I noticed that the playground sends the code from the playground to an API which executes it.

Then I thought if I can send my own request to that API and make it execute my code, then I can work on my idea around this. So I pulled out the API URL and the structure of the request payload, dropped them off into my Python script, wrote a sample Rust code, and tried sending it to this code execution API to see if it will work.

As you might probably guess, it worked.

"Gotcha!" I said.

So then I preceded to write this stupidly simple program that looks like Python's REPL, wherein it takes the user's raw code input and sends it directly to that API to execute the Rust code for us, then print the stdout or stderr that's returned by the API (if any).

Now, instead of opening the Rust playground, I can just pull out my terminal and run pieces of Rust code; without the need to create a new file and compiling it.

And since I'm pretty much satisfied (at least) with it, I thought why not share it to everybody else. There might be someone out there who also came from a background like me, and who used to have (and love) REPL or the interactive environment of their languages to test out some pieces of code.

So I decided to share it with the dev community.

You can check the repo of this stupid simple program I made here: https://github.com/menard-codes/rust_playground_locally

A little word of caution. I haven't tested this thing out, thus, there are potentially a number of edge-cases (as well as bugs) that'll make this not work properly.

Top comments (1)

Collapse
 
hady_eslam profile image
Hady Eslam

Great work, continue the work to improve it by not calling API and just compiling from terminal. And good luck