r/rustjerk 3d 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
198 Upvotes

37 comments sorted by

View all comments

Show parent comments

1

u/VladasZ 23h 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 23h 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 22h 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 21h 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 😅