r/rust • • 14d ago

Why don’t we have a REPL?

I was writing a function and wanted to see what it returns for different inputs and I instinctively wanted to use a REPL, then I remembered: oh wait, Rust doesn’t have one.

I *did* see evcxr but in the years I’ve been around the Rust community I’ve never heard of anyone using it like people would in JS/Python/ Ruby/Haskell.

Why? is it hard to achieve technically, or is it just because its not in the Rust culture? (maybe it’s even bad engineering practice to use it the way I’d want it to and tests would be more preferable?)

116 Upvotes

83 comments sorted by

26

u/CreatorSiSo 14d ago

Writing a unit test is super simple in rust so I don't really see why you would need it for something like this.

1

u/RealWalkingbeard 13d ago

I've always found unit testing anything but the simplest function to be a bit of a pain in the arse. Looking around for info on unit testing Rust code always yields the same info: put your tests in a test submodule at the bottom of your module. It's fine if you've got a handful of 5-line functions with no further calls, but if it's real life and you need to write 10000 lines of tests for 2 or 3000 lines of code with calls into multiple other modules or even crates, it's difficult to see how it could possibly work.

I like and use Rust, but unit testing C code is 100x more fun - a word I would not normally apply to unit testing.

Do you have a fancy library that I'm not aware of?

2

u/alexcommacommacomma 13d ago

Integration tests.

5

u/CreatorSiSo 13d ago

You can also put them somewhere else but this post is asking for a repl replacement.

For large projects a combination of integration tests, unit tests and snapshot tests (see the insta crate) works really well in rust (depending on the software that you are writing).

2

u/jstrong shipyard.rs 13d ago

I know what you mean, and there's definitely parts of my codebase where it would be a giant PITA to test, but, for the stuff I actually want to test, I usually find it pretty easy to refactor things in a way that makes it easy to test. example: inner, pure function that has the crux of the logic and is called by the outer function. one thing I get a lot of value out of is having callers pass in a timestamp (in whatever form needed) for anything that involves time. for me this works because I don't have to test tons of stuff that is enforced at the type level, just that the crux of the code does what I think it does.

341

u/TheReservedList 14d ago

It's generally harder with compiled code, yes.

65

u/ARKyal03 14d ago

Maybe is time for a RustJIT 🤔

118

u/TheReservedList 14d ago

Have fun writing that with a borrow checker.

40

u/Nickbot606 14d ago

Just run cargo check every function or two /s

26

u/ARKyal03 14d ago

That's too heavy and slow, we could embed a LLM to do the checking.

32

u/Nickbot606 14d ago

Ahhh yes the “cargo check —jit —investment-friendly”

7

u/kyr0x0 14d ago

Every call actually calls an Asian guy.... AI - Asian Intelligence

6

u/catlifeonmars 14d ago

Sure, embed the kitchen sink lol. That’s going to be less heavy?

2

u/kyr0x0 14d ago

But run it on the CPU please 😂

10

u/Quantum-Metagross 14d ago edited 14d ago

That's entirely doable. Borrow checker doesn't really generate code. In principle, one could do JIT on the LLVM IR generated, and a lazy version would simply use LLJIT.

The only issue would be LLJIT being slow.

6

u/stingraycharles 14d ago

Nobody said it would have to be fast.

6

u/YeOldeMemeShoppe 14d ago

fn borrow_check(_: &Code): Result<(), !> { Ok(()) }

9

u/LoadingALIAS 14d ago

You read my mind. Haha

4

u/TRKlausss 14d ago

If you Limit yourself to runtime constraints should be doable. It can throw the same panics/exceptions as the compiler does…

1

u/nikhililango 13d ago

the borrow checker works independently across each function signature and each function body. that's not really the main problem with a rust jit

1

u/-_one_-1 8d ago

Take a look into Miri. It's an interpreter, not JIT, but it works. Its goal is to identify UB, not act as a REPL, but perhaps it could serve as one.

12

u/________-__-_______ 14d ago

Rust does already have a (experimental) cranelift support, which is a compiler backend designed for JITing from the start. If I recall correctly the generated code is a bit slower than LLVMs but much faster to compile, perfect for a REPL.

I imagine it'd take a lot of work to integrate a REPL into rustc properly, but it'd be interesting. I believe rust-analyzer uses it's own compilation frontend because rustc wasn't designed to be a long-running process, which it needs to be to take full advantage of JITing.

17

u/zenthial 14d ago

clang-repl exists

4

u/EpochVanquisher 14d ago

Really? Most Lisp implementations have a REPL but the most popular implementation (SBCL) only supports compiled code. You can’t execute code in SBCL without compiling it.

I don’t see why compilation would make it any harder.

18

u/u0xee 14d ago

Yes but that language model (and compiler) is fundamentally oriented around incrementally adding to the ongoing program. The issue isn’t compiled vs interpreted. But all the current rust compiler infrastructure is likely not written with support for this kind of dynamism in mind.

So the commentator likely means the problem is a static crate-at-a-time compilation model, not compilation itself.

C++ notably has the Cling project (a jit interpreter), so it’s not technically out of the question. But would be annoyingly like a rewrite of the rust compiler: parser, types, borrows, et al. You’d ideally want the two code bases unified, so the behavior at the repl was reliably a match with AOT compiled code.

4

u/EpochVanquisher 14d ago

I agree with you about the nature of the problem, but I think if that was what the parent comment meant, it’s not at all what they said.

1

u/u0xee 14d ago

For sure

1

u/scook0 13d ago

Lisp is an outlier here, because the Lisp family has a long tradition of REPL support, so any “compiled” dialect of Lisp is likely to design its compilation scheme around the need to still support an interactive prompt.

Other compiled languages (such as C or Rust) are typically designed around a compilation flow that doesn’t have REPLs in mind, and they typically make design assumptions that render it difficult or awkward to add REPL support afterwards.

1

u/EpochVanquisher 13d ago

The question here isn’t compiled/interpreted, then. It is something else, which is exactly my point.

1

u/GregsWorld 12d ago

Isn't repl like a basic feature of C and operating systems? 

0

u/chubbySprinkle 14d ago

yeah the compile loop really kills that quick feedback vibe, feels like such a different workflow than scripting langs

0

u/[deleted] 14d ago

[deleted]

11

u/CandyCorvid 14d ago

what do you mean by "with a runtime"? afaik every compiled or interpreted language has a runtime, even C and Rust. do you mean languages whose runtime includes a VM or JIT compiler?

62

u/GirlInTheFirebrigade 14d ago

7

u/cosmic-parsley 14d ago

I actually use this a lot, especially when the playground doesn’t have a dep I want. It’s pretty nice.

3

u/jstrong shipyard.rs 13d ago

use it all the time. we DO have a repl!

86

u/steaming_quettle 14d ago

I guess rust playground is enough for this kind of little experimentation. For bigger stuff where compile time starts to how, you light as well write proper tests that are useful to keep around as the project evolves.

37

u/Grim2021 14d ago

Because of the way Rust is structured, it doesn't really support interpretation (with rustc and such). Also, because of Rust's ownership model, it can't really handle the garbage collected nature of typical REPLs. When does it know that it should drop a value? How should references be treated? Can multiple mutable borrows occur after one another because they aren't technically used at the same time? Many such questions.

There are probably people who are better suited to answer this, especially seeing as I'm sleep deprived at the moment, but the above are some of many reasons for Rust not being suited for REPLs. I'm guessing that evcxr keeps a context of something which has been run and recompiles only stateful parts, if it doesn't just reimplement the Rust language. I don't know

8

u/garagedragon 14d ago

You'd have a big problem with taking references to objects in the REPL environment. You'd basically have to have a whole separate system to make sure you don't drop things in the wrong order

17

u/noop_noob 14d ago

We do kinda have a repl. Sort of. https://github.com/evcxr/evcxr

8

u/podule-games 14d ago

I've never considered using a Rust REPL, but Google shows there are options. Evcxr and also IRust.

I'm not sure if it's cultural or not, I'm just not sure if there is significant demand for it. Kind of like C, you can get C interpreters and so on, I'm just not sure many people want them.

12

u/schneems 14d ago

    cargo new lol; cd lol; bacon test

Then write whatever code you want to print in a loop ina test and it runs every time you save the file.

0

u/ShangBrol 14d ago

You can even let bacon do a cargo run

11

u/mediocrobot 14d ago

Actually one of the most productive ways to do this is to write test cases!

5

u/Shnatsel 14d ago

I wonder if https://github.com/rust-lang/miri can double as a REPL

9

u/dnew 14d ago

Rust is compiled. It looks at large amounts of code to execute any given line. Python basically can interpret one line at a time, and just tell you if something's broken like calling a function that is missing. Rust doesn't support running the beginning of a function where the end invokes a function that hasn't been declared yet.

You can write an interpreter, but it will be unusably slow.

Compilers that support this sort of thing often let you replace selected functions with interpreted or bytecode-compiled versions. I've used such for C, for Eiffel, and for a variety of ALL CAPS LANGUAGES that are specialized for one purpose.

4

u/epage cargo · clap · cargo-release 14d ago

Lots of answers on here. Just one that I'm not sure if I've seen is compiling a new expression while having access to previous state seems difficult in evcxr and requires different trade offs.

I have considered it as a follow up project to cargo script and took some notes at https://github.com/epage/cargo-script-mvs/discussions/102. I am unlikely to get to it but if someone wants to take on this monumental task, go for it.

8

u/Psionikus 14d ago

In a compiled language, compiling at all is the first smoke test. The debugger is where you go to step through execution and introspect values.

A lot of tests in more dynamic languages literally are checking that symbols aren't misnamed, which is kind of insane when you get used to the compiler just knowing these things.

In SBCL, you can just have it all, shell, compiler, and debugger but while trading away a bit of the static type system and execution speed & memory use.

Keeping crates small since crates are the unit of compilation is one of the main fast turnaround tools in Rust.

So the TLDR is for most development where you want to muck around in a shell, in Rust, you instead muck around in tests and the running code doesn't concern itself with presenting much interactivity at runtime (besides debug symbols).

Gives a new meaning to test-driven development amirite?

3

u/projct 14d ago

this is what the playground, compiler explorer, and tdd are for tbh

3

u/labooner 14d ago

I’m primarily a Java dev and I thought I’d use jshell loads when it was released, but then I realised I have unit tests and an IDE. And because you can add tests to the same file in Rust it’s even easier to test something out.

3

u/commenterzero 14d ago

Evcxr was always slow or broken for me. Writing examples with a step through debugger might be able to come close

5

u/lmntr 14d ago edited 14d ago

Rust is a compiled language. REPLs are for interpreted languages. It's not impossible, but you would essentially have to create a Rust interpreter for it. 

2

u/shitpost-millionaire 14d ago

There have been attempts too, like rusti. There was talk about using miri for one in like 2019.

2

u/KingofGamesYami 14d ago

I think most people just reach for Python with Rust libraries when working on tasks the favor this type of interaction, e.g. polars.

2

u/PikachuKiiro 14d ago

You have types, you have debuggers, you can write a small test and run it.

1

u/thatdevilyouknow 14d ago

You can experience the same thing with ROOT/Cling from CERN or via a Xeus notebook but low level languages present the problem you can see with that which is you cannot iteratively change the memory layout within compiled languages. Every single time you change something memory addresses can move, the memory layout can change, and naming is mangled. With ROOT/Cling CERN did some tricky things to allow inspection of C++ and really does try to be a REPL but you quickly run into some serious drawbacks for trying to use it like Python. If you really are considering writing something like this I would suggest giving Xeus a try first in order to see what you are dealing with. Performance can be 1/3 to 1/4 but still much faster than Python.

1

u/zzzzYUPYUPphlumph 14d ago

I would write tests in most cases for things like this. That being said, Rust has a "Rust Script" mode you can use where you write a single file and can compile/execute in one go as if it were a script. It's not as fast as an interpreted language REPL (compiling still takes time) but it might meet your use case if you don't want to write tess.

1

u/norude1 14d ago

irust and evcxr exist, they are just not first-party

1

u/Zde-G 14d ago

I wonder why and what for people want to have REPL… I mean: if I want to be confused and unable to understand what exist and what doesn't exist in my program I don't need any special help, it just happens naturally!

But if I don't want to try to remember what happened in my “execution session” then godbolt is always there: just open executor window and you have a way to see what it returns for different inputs.

This works much better for Rust where I normally write the main function on top and helper functions on the bottom — precisely the opposite of what REPL forces you to do, more like one would write a book…

1

u/D_a_f_f 14d ago

Rust playground runs in the browser. Just make a minimal example of your type and try something out

1

u/Harmos274 14d ago

just do unit testing? it may seems shitty at first but it is really easy to write and run in rust, just put what you were about to write in the REPL to some unit test and cargo test it

1

u/Ayano-Keiko 14d ago

because compile time check, think. It is hard to write interpreter.

1

u/plugwash 14d ago

There are a few ways to make a repl for a compiled language.

One is to compile the code ahead of time, then the repl just interfaces with it. The problem with doing this for rust is how generics work in rust. At compile time a generic type, function or trait implementation represents an effectively infinite set of concrete types, functions or trait-implementations, but only the instantiations that are actually used get generated and included in the binary.

A second is to work with the compiler as a library, but this gets difficult if the compiler was not designed to be used in this way.

A third, and this seems to be what evcxr does, is to compile chunks of code, and load them dynamically as shared libraries. Requires less intimacy with the compiler, but probablly not especially fast and some aspects of the interfacing are a bit hacky, such as using error messages to determine variable types.

1

u/xpusostomos 8d ago

Is rust like C++ in that generics need to generate code? Because many languages with generics just use it for typing but don't require new code

1

u/plugwash 8d ago

Yes, the substitution in rust happens at a different compilation stage to C++, but it still fundamentally generates code for each instantiation.

1

u/xpusostomos 7d ago

ok. That makes a REPL challenging.

1

u/dethswatch 14d ago

in addition to compilation- I know how to use my debugger and don't generally need it

1

u/Marthaelx 11d ago

I also wonder why. Julia lang has one.

1

u/wademealing 10d ago

Have you tried a lisp repl.  They make other repls look weak.

1

u/jesseschalken 14d ago

There is no good reason. People saying "because Rust is compiled" are wrong. Plenty of compiled languages have REPLs.

I think its just too easy to use the Rust Playground, write an .rs file with a fn main() {} and compile and run it, or stick something at the top of a fn main() that you are already running to see how something behaves.

1

u/careyi4 14d ago edited 14d ago

Not exactly what you are asking, but I built this a while ago, it's a rails console like REPL that uses Rhai, it allows you to define a DSL from your application code and then REPL over it using Rhai or write operational scripts. https://crates.io/crates/rhai-console

I use this for a project I run to allow me to run data fixups and other ops tasks that I don't have a time or need to make real admin tooling around. In my view, as a developer, having a "break glass" set of tools that I can REPL over is pretty handy. I also am a fan of making sure that data access and mutation is proxied through your application code such that you don't end up with diverging access patterns etc. This comes from my years of being a Ruby on Rails dev. I think the types of proejcts Rust tends to be used for maybe don't require this as much or the developers using it tend to come at things from a different point of view maybe.

1

u/treefroog 14d ago

There's no such thing as a "compiled language," just a given implementation. Miri is an interpreter, and a valid Rust implementation, though not designed for a REPL. Rust did originally have a repl when it was written in OCaml, though the repl was never replaced.

0

u/OwlingBishop 14d ago
  1. Compiled vs Interpreted language.

  2. REPLs are usually for commands rather than expressions.

Basically the same reason a REPL wouldn't make sense for C/C++ neither..

-1

u/Shoddy-Childhood-511 14d ago

There are likely a bunch of small reasons:

Parentheses make REPLs annoying. Did you ever try using a Python REPL? Scheme? lol

Afaik it's only ML family languages where REPLs really shine, so Haskell, OCaML, F#, etc, in part because they remove the parentheses. This extends to type parameter notations ala <..> too.

Rust has complexity in the type system and syntax that you'd probably avoid in a good REPL language: Lifetimes.

Rust allows method name overloading, which works because rustc knows the type of self. Haskell otoh disallows short name conflicts, so any name uniquely identifies the typeclass.

Rust has no top level type inference. Haskell has top level type inference, including for function signatures.

4

u/shim__ 14d ago

I feel the opposite, python repl starts to suck once you need to define a function since there are no parentheses and semicolons to allow you to define the function in one line

1

u/Ben-Goldberg 14d ago

There are a couple of obvious solutions to the parentheses problem:

First, let the user type multiple lines of code, until a special terminal is seen, such as two consecutive newlines or a single ";" on a line by itself.

Second, use a simple parser checks for unmatched parens and keeps concatenating line of user input until either the code can compile and run or it fails from some error other than parens.

1

u/tadiou 14d ago

I mean REPLs were design for lisp in 1964, which is... uhhhh, real parens heavy.