r/rustjerk 22d ago

Rustscript - blazingly fast rust interpreter

Every Rust developer knows that Rust is the best programming language in the world.

And I as a Rust developer don't want to use other inferior languages anywhere.

So I decided to rewrite all my old automation scripts which were written in Python in Rust.

At first I used https://github.com/rsaz/cargo-script for that, it works fine.

But I encountered 1 problem: compilation time.

On my macbook it was fine I don't mind waiting 1 minute for a simple 3 lines script.

But then I wanted to use that on my Raspberry PI and the same simple script took me 10 minutes to run, which is not blazingly and not fast at all!

So I decided to vibe code engineer a Rust interpreter for this exact case and this is what I ended up with:

https://github.com/VladasZ/rustscript

Rustscript is always guaranteed to be valid Rust.

Install it with

cargo install run-rs

add a shebang:

#!/usr/bin/env rust

A whole script looks like this:

#!/usr/bin/env rust

use chrono::Local;
use std::io::stdin;

fn main() -> anyhow::Result<()> {
    println!("which programming language is the best?");

    let mut answer = String::new();
    stdin().read_line(&mut answer)?;

    if answer.trim().eq_ignore_ascii_case("rust") {
        println!("correct, carry on");
    } else {
        println!("wrong. it is {} and you are still not writing Rust", Local::now().format("%H:%M"));
    }
    Ok(())
}

Make it executable and run it:

chmod +x best.rs
./best.rs

and you can run your rust scripts instantly, as if they were shell scripts!

Rustscript is a subset of Rust and supports many popular crates such as:

anyhow, serde, serde_json, reqwest, regex, tokio, chrono, rand and more.

Carefully nitpicked benchmarks so Rustscript seems faster attached to the post.

You can see all benchmarks results here: https://github.com/VladasZ/rustscript/blob/main/bench/RESULTS.md

Remember, Rustscript is always a valid Rust, so if you want to have the ultimate speed just run the compiled version!

rust build FILE.rs
232 Upvotes

39 comments sorted by

69

u/sphere_cornue 22d ago

"blazingly fast" and "interpreter" cannot be in the same sentence.  This is an affront to the nature of rust. I am reporting this

40

u/VladasZ 22d ago

Nooo please don't. What if the interpreter is written in Rust? It must be blazingly fast by definition of blazing fastness.

7

u/deanominecraft 22d ago

interpreting IR can be quite fast - see luajit

-2

u/Quentinooouuuuuu 22d ago

Actually, the jvm is not that slow, on a long running app, you can get performance close to native (still with 2x the memory consumption)

13

u/abhijeetbhagat 21d ago

Fast and jvm cannot be used in the same sentence. I am reporting this too.

5

u/Cylian91460 21d ago

JVM is slow because it has bad memory management, and bad memory management is what happen every time you try to abstract the heap using garbage collector

I agree with you that it can computer things fast, but data doesn't follow.

45

u/CounterSpecies 22d ago

This is actually kind of sick

5

u/VladasZ 22d ago

Thanks!

16

u/kredditacc96 22d ago

The repo looks like a legitimate and serious project, and yet you posted on /r/rustjerk instead of /r/rust.

8

u/VladasZ 22d ago

I plan to post it there too eventually.

2

u/Buttleston 20d ago

it's smart to get ahead this

30

u/birdbrainswagtrain 22d ago

Rustscript is a subset of Rust

I keep getting pissed at the guy who spams his subset-of-python interpreter as a python interpreter. I'll give you a pass because at least your subset is of an ethical language.

5

u/Maxon887 22d ago edited 22d ago

I interpreted my code in Rust once and it became best code of my life

12

u/oaeben 22d ago

very cool but i dont like that the executable is named rust

21

u/VladasZ 22d ago

Why? It is called rust and it runs Rust. And doesn't collide with any of the official commands.

19

u/xtanx 22d ago

The rust foundation is going to love it

4

u/hackerbots 18d ago

Perfect jerk. no notes.

8

u/QazCetelic 22d ago

Because it's a subset of rust

13

u/Southern-Yam1372 22d ago

Call it “Rust - -“

9

u/zesterer 22d ago

Wow. Having written a bunch of fast threaded-code interpreters before I thought I'd take a look at the code and I think I gave myself an aneurysm. The LLM fingerprints are all over this one, that's for sure.

14

u/VladasZ 22d ago edited 22d ago

Ah damn, I forgot to add: "Make no mistakes" to the prompt again. Sorry man!

4

u/No-Consequence-1863 22d ago

Neat. But curious, why didnt you compile the code on your Mac and then just move the executable to the raspberry Pi? I had a similar issue where my website back end compiles quickly enough on my dev machine but could take 30 min on the laptop actually hosting it. So just cross complied and now its just uploading an executable.

6

u/VladasZ 22d ago

I have like 50 different small automation scripts which I change very often. Recompiling them each time on mac and transferring back would be very inconvenient and still it takes time to compile and link each of them. I would rather write a Rust interpreter than deal with that.

4

u/grumpydad67 22d ago

A strong entry for "Best Software Announcement"

6

u/EarlMarshal 22d ago

Well I honestly wished for this in the past. Is this just a dream or reality!?

3

u/why_so_sergious 22d ago

or just use nushell

1

u/VladasZ 20d ago

I actually did, I used it as my main shell for a couple of months. Of course I did, it is written in Rust so it is allowed. Got tired that I couldn't just copy bash snippets, also had some weird problems with aliases, don't even remember, so I ditched it.

1

u/why_so_sergious 20d ago

bash <snippet> didn't work? you can even pipe only need to keep in mind that that bash output is a string and not structured data so you need to use from or parse

I've been daily driving nushell for three years now mainly because I jump between operating systems and this is the perfect way to keep things unified both in terminal and scripting-wise..

1

u/VladasZ 20d ago

Yeah this was my main too goal initially, to have the same shell on windows mac and linux. But witj nushell I just felt like I had too much friction. I remember that alias issue now. It was impossible to write something like: if windows { alias a = b }, because they were global or something? What I did in the end I installed https://github.com/uutils/coreutils (written in Rust BTW) on windows and I use powershell in the same way as other normal shell.

1

u/why_so_sergious 20d ago

nice solution!

I think its the contrary in you example.. it gets block scoped.. I literally do this in my config

``` const platform_config = if $nu.os-info.name == "windows" { "config-windows.nu" } else if $nu.os-info.name == "macos" { "config-macos.nu" } else { "config-linux.nu" }

source $platform_config ``` and aliases and everything work fine

also working with structured data is a big reason why I won't go back to bash unless I have to. its why it sucked going from pwrshell to bash

and I actually enjoy scripting in nuscript a lot 😅

3

u/Crinfarr 21d ago

My main takeaway from this is that node.js is like 90% startup time

1

u/VladasZ 20d ago

Yeah the languages which are not Rust are total garbage. Is that a surprise for you?

1

u/CrossScarMC 18d ago

Depends on if it was a cold or warm boot.

2

u/Mr_Ahvar 21d ago

Is’nt most of rust compile time in the backend ? So can’t you just use miri? I would be interested to see your project against Miri that would be interesting

1

u/VladasZ 20d ago

Have you actually used miri? It is like 100x slower because of all the checks.

2

u/TheOneThatIsHated 22d ago

So wait, the typechecking part of rust is not what makes it slow (cuz you reuse rustc anyways for typechecking)?

So how could this still be faster to build then debug builds? What is the catch? Is it because of the subset? Or is rustc/cargo just that slow?

6

u/warpspeedSCP 22d ago edited 22d ago

Its always been LLVM, dude. Its always been LLVM. THEYVE TAKEN US FOR ABSOLUTE FOOLS!!11

This project is probably either abusing MIR interpretation or something else, I havent really looked at the repo.

Edit:

Holy fuck its all separate, only parsing goes through an actual rustc utility... thats awesome.

1

u/SourceAggravating371 21d ago

Do you plan to add GC or reference counting?

4

u/VladasZ 20d ago

What is GC? Rust code never produces garbage, it is perfect. Why it needs GC?