I found a fun tools in dotnet, called
System.Linq.Expressions, which allows to convert user input into a compiled code at almost no performance loss.
I've heard that, programming languages like Julia, which is also based in LLVM, could also compile the expression user typed.
Is there something in rust now? Although I've found some crate like
expr, but they does not provide a compiling option, and which results to a huge performance loss.
I have no real use cases yet, just curious about it.
.NET, or more precisely, CLI, can support this because it is a “virtual machine” platform including a just-in-time compiler, meaning that once source code is translated into a common intermediate representation (“bytecode”) the remaining compilation steps are performed at run-time, in exactly the same way as happens when an ordinary program is loaded into the virtual machine.
The cost of this architecture is that:
- the virtual machine is a substantial program itself, which is usually installed separately from the application, and
- the program must be JIT-compiled each time it starts.
Rust instead chooses entirely “ahead-of-time compilation”, which allows smaller, dependency-free programs that start quickly, and libraries that can be loaded into and called by non-Rust programs without complications.
That said, it is possible to evaluate Rust code if you try hard enough: check out evcxr. However, this is a complex workaround: each time you write a new expression, it's compiled by the regular Rust compiler into a dynamic library which is then immediately loaded by the host program. This approach is suitable for interactive tinkering, but not for more general use because it is quite expensive per expression (and I'd guess but am not certain that the code is all “leaked”, i.e. not unloaded when it's done executing, because it might contain functions that are still referenced).