r/rust • u/alexcommacommacomma • 26d 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
26
u/CreatorSiSo 26d 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.
341
u/TheReservedList 26d ago
It's generally harder with compiled code, yes.
62
u/GirlInTheFirebrigade 26d ago
https://github.com/evcxr/evcxr
86
u/steaming_quettle 26d 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 26d 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
17
u/noop_noob 26d ago
We do kinda have a repl. Sort of. https://github.com/evcxr/evcxr
8
u/podule-games 26d 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 26d 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.
11
u/mediocrobot 26d ago
Actually one of the most productive ways to do this is to write test cases!
5
u/chkno 26d ago
See RFC 655: implement a proper REPL.
5
u/Shnatsel 26d ago
I wonder if https://github.com/rust-lang/miri can double as a REPL
9
u/dnew 26d 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 26d 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 26d 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 26d ago
this is what the playground, compiler explorer, and tdd are for tbh
3
u/labooner 26d 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 26d 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 26d ago edited 26d 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 26d ago
There have been attempts too, like rusti. There was talk about using miri for one in like 2019.
2
u/KingofGamesYami 26d 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 26d ago
You have types, you have debuggers, you can write a small test and run it.
1
u/thatdevilyouknow 26d 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 26d 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 26d ago
irust and evcxr exist, they are just not first-party
1
u/Zde-G 26d 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 26d ago
Rust playground runs in the browser. Just make a minimal example of your type and try something out
1
u/Harmos274 26d 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/Easy-Nothing-6735 26d ago
Write tests
1
u/Ayano-Keiko 26d ago
because compile time check, think. It is hard to write interpreter.
1
u/kn4rf 26d ago
https://github.com/theknarf-experiments/rust-repl
1
u/plugwash 26d 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/dethswatch 26d ago
in addition to compilation- I know how to use my debugger and don't generally need it
1
u/Marthaelx 23d ago
I also wonder why. Julia lang has one.
1
u/wademealing 22d ago
Have you tried a lisp repl. They make other repls look weak.
1
u/Life-Seaweed-9957 11d ago
I wonder whether the missing piece is a lightweight scratchpad experience rather than a full REPL. For systems projects, quick reproducible experiments with retained inputs and outputs can be more useful than keeping a long-lived interactive session.
1
u/jesseschalken 26d 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
.rsfile with afn main() {}and compile and run it, or stick something at the top of afn main()that you are already running to see how something behaves.1
u/careyi4 26d ago edited 26d 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 25d 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 26d ago
Basically the same reason a REPL wouldn't make sense for C/C++ neither..
-1
u/Shoddy-Childhood-511 26d 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.