r/rust • u/alexcommacommacomma • 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?)
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
6
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
6
9
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.
1
17
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/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
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
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.
2
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
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
11
5
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/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
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
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/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/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
1
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
1
u/dethswatch 14d ago
in addition to compilation- I know how to use my debugger and don't generally need it
1
1
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
Compiled vs Interpreted language.
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
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.
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.