Java 2 Rust "compiler"

I have made available a naive "compiler" to help in java to rust translation.

2 Likes

I don’t really understand the premise of this. Sure it’s a “naive” approach, bit in your project’s case this seems to mean that it doesn’t even create valid Rust syntax in most but the most trivial cases, at least judging by your example Employee.rs. And even if one improves upon the most obvious shortcomings, it’ll never be possible to just straightforward compile Java to Rust.

The lack of GC and real OO in Rust means that many Java idioms don’t translate directly into Rust, and on the other hand Java’s lack of Rust features means that the results will never be nowhere near realistic/useful Rust code. Even if you manage to get the result of such a translation “working” with—perhaps—some manual work, too, the code will never be structured in a way that’s idiomatic for Rust.

I recently read a thread discussing C to Rust compilation which, both being “systems programming” low-level languages and Rust supporting pretty much everything that C can do, might seem like a reasonable thing to try, but even that isn’t without problems. To quote an interesting reply in this topic:


So IMO, while this probably is, of course, a fun toy project, and maybe a great way for you to learn more about Rust syntax / the Rust language in general while you’re implementing this “Java 2 Rust “compiler””, I highly doubt that it can ever productively “help in java to rust translation”, as advertised.

2 Likes

Sorry, it was not my intention to bother anyone.

I used it to convert a small set of DAO classes and it somewhat help me in the process along a structure that mimics jdbc behavior.

Not a bother. Btw, what's DAO?

DAO is Data Access Object.

It is a common pattern in JAVA that concentrates all database access in a single place.

Like EmployeeDAO that would create, read, update, raiseSalary, fire in database table Employee.

Unlike ActiveRecord pattern an EmployeeDAO instance is not an employee instance, it returns employee instances.

2 Likes

Not a bother at all. I just wanted to make sure the expectations weren't unrealistic.

Ah, well that's pretty nice if it actually helped you in some cases.

You quote me, but I'd say that my reasons for being disappointed with C to Rust translations were C specific. Java may be tricky for other reasons (use of inheritance, shared mutable references), but maybe these are easier to solve?

But you were right. It is not a good piece of code.

Tried to compile in a single pass and paid no attention to important things. Like you said: a toy project.

I starting improving it, building an intermediate tree that you allow useful tricks like copying String data returning from functions, to not violate borrow checkers.