<p>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 &amp;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.</p>
<p>Is it technically possible to write a transpiler to Rust that can automatically create the right borrowing and lifetime specifiers?</p>
<p>I am still in my learning journey on these things, so pardon me if the question is naive.</p>

<rust><memory-safety><high-level-languages><transpilers>
Creating a high level language that transpiles to rust
CC BY-SA 4.0
2023-06-22T02:18:16.300Z
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.
Bbrk24
CC BY-SA 4.0
2023-06-22T02:29:29.823Z
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](https://languagedesign.stackexchange.com/q/835/89).
kaya3
CC BY-SA 4.0
2023-06-22T02:32:58.700Z
The other thing is, Rust has famously slow build times, so a language which compiles via Rust will be even slower to build programs.
kaya3
CC BY-SA 4.0
2023-06-22T03:10:17.887Z
@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?
Ong Teck Wu
CC BY-SA 4.0
2023-06-22T03:13:40.620Z
I don't know enough about Lobster to say; I will need to investigate it further as it's relevant to my own interests too. I think the difficulty with this question is that you're asking for a yes or no answer (is it technically possible?) when I doubt that a yes or no answer would actually tell you what you want to know. So it would be best to edit your question to say exactly what information you're looking for in an answer.
kaya3
CC BY-SA 4.0
2023-06-22T08:17:28.227Z
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).
SK-logic
CC BY-SA 4.0
2023-06-22T20:52:04.407Z
And more, Rust have procedural macros, meaning macros can be compilers for embedded DSLs. So it is very important to have the right tools for translating all kinds of different languages into baseline Rust in order to be able to build rich eDSL environment.
SK-logic
<p>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 <em>pre-processor</em>. The original C++ compiler and the first Objective-C compiler are examples of this.</p>
<p>But if you want the normal sort of compiler checking and error handling that you expect, you need to do semantic analysis, and <em>that</em> part will not be trivial or easy, if you want to do automatic annotations and access the same low level information as Rust does.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2023-06-27T19:50:12.003Z
2023-06-27T19:50:12.003Z
Nuoji
CC BY-SA 4.0
2023-08-06T15:03:56.820Z
At that point you might as well *not* transpile to rust
Seggan
# Creating a high level language that transpiles to rust
Posted by: username_0 on 2023-06-22
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.
**Tags:** rust memory-safety high-level-languages transpilers
> **Comments:**
> **username_1**: 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.  
> **username_2**: 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](https://languagedesign.stackexchange.com/q/835/89).  
> **username_2**: The other thing is, Rust has famously slow build times, so a language which compiles via Rust will be even slower to build programs.  
> **username_0**: @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?  
> **username_2**: I don't know enough about Lobster to say; I will need to investigate it further as it's relevant to my own interests too. I think the difficulty with this question is that you're asking for a yes or no answer (is it technically possible?) when I doubt that a yes or no answer would actually tell you what you want to know. So it would be best to edit your question to say exactly what information you're looking for in an answer.  
> **username_3**: 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).  
> **username_3**: And more, Rust have procedural macros, meaning macros can be compilers for embedded DSLs. So it is very important to have the right tools for translating all kinds of different languages into baseline Rust in order to be able to build rich eDSL environment.  
---
## 1 answers
### Answer by username_4 on 2023-06-27. Score: 4
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.
> **Comments:**
> **username_5** (2023-08-06): At that point you might as well *not* transpile to rust  
---
