-1
$\begingroup$

I really like what Rust brings to the table, but I find it very difficult to work with if I am not really working on apps that require absolute speed and the lower-levelness e.g. specifying int sizes, specifying &str or String, having to attach to vector to a variable first before passing it into an argument. I really just want the compiler to do most of heavylifting for me. e.g. prototyping or web development.

Is it technically possible to write a transpiler to Rust that can automatically create the right borrowing and lifetime specifiers?

I am still in my learning journey on these things, so pardon me if the question is naive.

$\endgroup$
7
  • $\begingroup$ If you're doing web development, you basically have to target JavaScript. WebAssembly exists, but it can't (currently) interact with the UI in any way. $\endgroup$
    – Bbrk24
    Jun 22, 2023 at 2:18
  • 2
    $\begingroup$ The trivial answer is yes, so long as your language is very similar to Rust it will be straightforward to compile to Rust, and the more differences your language has, the harder it will be. If you want the user to not have to specify ownerships and lifetimes in their code, then your language will be substantially different and it will be difficult to compile to Rust; the hard part will be what this other question is about. $\endgroup$
    – kaya3
    Jun 22, 2023 at 2:29
  • $\begingroup$ The other thing is, Rust has famously slow build times, so a language which compiles via Rust will be even slower to build programs. $\endgroup$
    – kaya3
    Jun 22, 2023 at 2:32
  • $\begingroup$ @kaya3-supportthestrike thanks Kaya I really appreciate the link. Didnt know Lobster is trying to solve what Im looking for. In your opinion, do you think Lobster is solving for what Im looking for? $\endgroup$ Jun 22, 2023 at 3:10
  • 2
    $\begingroup$ Not sure why it is closed, it is a very good question with some rather deep consequences. I'd say, yes, it is possible to have a target language with complex borrow checking semantics, but it is quite challenging for the source language compiler. Let's assume it's a bog standard functional programming language with an assumption of GC present (i.e., not caring at all about allocation, ownership, etc.) - in this case you'll need a full program region analysis and some run-time help (e.g., an implicitly injected ref counting GC). $\endgroup$
    – SK-logic
    Jun 22, 2023 at 8:17

1 Answer 1

3
$\begingroup$

If you envision your program to simply parse and then transpile to Rust, then it is possible to do so, but you can then only error report in the parsing and transpilation. This is essentially then a pre-processor. The original C++ compiler and the first Objective-C compiler are examples of this.

But if you want the normal sort of compiler checking and error handling that you expect, you need to do semantic analysis, and that part will not be trivial or easy, if you want to do automatic annotations and access the same low level information as Rust does.

A very simple example: let's say you want to retrieve the size of a struct at compile time - this may be part of the transpilation or compile time checks.

To do so your frontend needs to understand (1) how structs are laid out (2) how the types in the structs are laid out and their sizes (3) how this together can be calculated into some size.

You also need to think about how those high level constructs should be translated to Rust, to make sure you are able to translate them well, but that's a different question.

$\endgroup$
1
  • $\begingroup$ At that point you might as well not transpile to rust $\endgroup$
    – Seggan
    Aug 6, 2023 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .