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 …


This content originally appeared on DEV Community and was authored by Menard Maranan

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 it comes to 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 locally.

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 check 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.


This content originally appeared on DEV Community and was authored by Menard Maranan


Print Share Comment Cite Upload Translate Updates
APA

Menard Maranan | Sciencx (2022-07-16T22:40:00+00:00) REPL-like environment for Rust (A Rust Playground Locally). Retrieved from https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/

MLA
" » REPL-like environment for Rust (A Rust Playground Locally)." Menard Maranan | Sciencx - Saturday July 16, 2022, https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/
HARVARD
Menard Maranan | Sciencx Saturday July 16, 2022 » REPL-like environment for Rust (A Rust Playground Locally)., viewed ,<https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/>
VANCOUVER
Menard Maranan | Sciencx - » REPL-like environment for Rust (A Rust Playground Locally). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/
CHICAGO
" » REPL-like environment for Rust (A Rust Playground Locally)." Menard Maranan | Sciencx - Accessed . https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/
IEEE
" » REPL-like environment for Rust (A Rust Playground Locally)." Menard Maranan | Sciencx [Online]. Available: https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/. [Accessed: ]
rf:citation
» REPL-like environment for Rust (A Rust Playground Locally) | Menard Maranan | Sciencx | https://www.scien.cx/2022/07/16/repl-like-environment-for-rust-a-rust-playground-locally/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.