ChatGPT Experiment: Migrating a Project from JVM to Rust? (3/4)

Patrick Jung
arconsis
Published in
8 min readMay 4, 2023
Cover Image created by Midjourney

As the year 2022 drew to a close, a new era of excitement and innovation began with the release of ChatGPT by OpenAI. From tech enthusiasts to industry experts, people around the globe are exploring the full range of capabilities offered by this cutting-edge technology. In this series, we’re taking a look at different aspects of the new AI tool to evaluate how it could help developers in their daily life.

Over the past month, we’ve been experimenting with ChatGPT as a virtual assistant for software developers and evaluating different ways to use the tool in our daily lives. If you haven’t read the series yet and are interested, feel free to check out our previous articles about ChatGPT after finishing reading this one (of course 😉):

Besides ChatGPT, we’ve also had our colleagues Evangelos and Alexandros reviewing and benchmarking Quarkus vs. SpringBoot, which is definitely worth a read as well:

We don’t want to spoil too much here, but looking at the benchmarking numbers, we quickly thought about how the JVM frameworks compare to something that is running more native… — like Rust maybe?

We have some colleagues at arconsis who like to use Rust, but since they don’t have the time to implement the benchmark on short notice, I thought about a different way to achieve this goal: Using our beloved new virtual assistant ChatGPT to create the code — and me as the one who puts it all together.

The Given Situation

Before going into more detail about how the experiment itself went, let’s take a moment to talk about what the bookstore service we’re trying to migrate actually is:
It’s a simple REST service with CRUD operations, deployed in a cloud-native environment on AWS Elastic Container Service, rather than a specifically prepared environment. We’re using this setup to have a real-world-like scenario to measure the runtime performance of certain languages and frameworks, such as Kotlin and Quarkus.

With that out of the way, let’s talk briefly about the idea that we had in mind. Since ChatGPT is able to understand Kotlin and is also able to generate Rust, it was pretty simple:

“Just copy the Kotlin classes into ChatGPT, generate the equivalent in Rust, put it all back together, and off we go.”

That’s the theory. But how well does it work in practice?

Disclaimer: It’s worth mentioning here, that this was the first time ever I’ve come into contact with Rust. Even though we at arconsis love exploring and trying out new technologies, for the sake of this experiment, I deliberately tried to avoid reading the documentation — for both — Rust itself and also the frameworks we’re about to use.

But why?

There’s an actual reason for that:

We want to evaluate if it would be possible to create a project clone or migrate an entire project from one programming language to another, without actually being able to use or understand that language at all.

Of course, we’re all software engineers, and of course, we (should) know how to code, but that doesn’t necessarily mean that we’re able to read and write in any language we want without having to learn the basic language features or additional platforms and frameworks. So it’s going to be an interesting journey — let’s get started!

Into the Wo(u)nderland (yes, it was painful)

Since we can't just drag and drop a whole project into ChatGPT and expect it to generate the equivalent in a different language, we needed to come up with a plan to go through all the steps:

  1. Install Rust and create a project setup
  2. Find equivalent frameworks for our requirements (Webservice & Database)
  3. Migrate the code (aka. have ChatGPT convert it to Rust)
  4. Put it all together
  5. Compile, execute & benchmark

Install Rust and create a project setup

Since this is not a tutorial, we’ll skip the part about installing & setting up Rust. The only important part to mention here is that we’ll be using cargo for the project setup.
In case you don't know,cargo is the npm for Rust (aka a package manager). How do we know? Well, we asked ChatGPT:

ChatGPT explaining how to set up a new Rust project

Thanks to ChatGPT’s explanation, we now know how to create the new project setup and what the files it generates are used for. This is quite handy as it will help us in future steps when we need to add our dependencies and start converting and assembling the code.

Find equivalent frameworks for our requirements

We continued with finding the right frameworks for the requirements. The bookstore itself is a pretty simple implementation for benchmarking, so we basically only need two things:

  • A web services framework that can handle our client requests
  • A database framework that we can use to read/write our data to a persistent volume

So let’s continue the journey and ask ChatGPT for some good alternatives that we can use. Ideally, the frameworks should be easy to use and well-known/maintained. We’ll start with the yet-to-be-determined web framework we “want” to use:

ChatGPT introducing Rocket Framework

Looks like solid code at first glance, right? But remembering that ChatGPT is only trained on data up to 2021, we better double-check that the dependency version is up to date (spoiler alert: it wasn’t).

After adding Rocket to our dependencies, we can proceed with the next framework we need: Something to access the Postgres database.

What’s a good and easy to use database framework that works with rocket and is also performant

ChatGPT explaining how to use Diesel, an ORM library for Rust

Nice – there’s an ORM library in Rust as well, so we should be good to go. By adding Diesel to the dependencies, we now have all the necessary frameworks we need to use and can continue with the next part.

Migrate the code (aka. letting ChatGPT convert it to Rust)

ChatGPT is context-aware and already knows what we’re trying to achieve: A web service that is able to serve some requests and also query and persist data. All we have left to do now is give it the Kotlin code as input and use the generated output in our project setup. In this example, we’ll use the Quarkus variant of our bookstore service to generate the Rust equivalent of the required classes we need:

  • A controller that provides all the endpoints
  • A repository that’s able to query & persist data
  • Some sort of mapping from the DB entity to the JSON response model (ideally)

We’ll start with the BooksResource and instruct ChatGPT to generate the equivalent of the Kotlin implementation in Rust:

ChatGPT’s generated Rust output based on Kotlin input

We’ll put it in our main.rs and keep it there for later, as we still need to create the database functionality that we’re using in the generated code above. Otherwise, the code will be simply invalid and won’t compile due to missing declarations & functionality.

Let’s continue with that. We’re using the BooksDataStore implementation from the Quarkus project to have ChatGPT generate an equivalent for this in Rust.

ChatGPT trying to convert the BooksDataStore from Kotlin to Rust

Once again, it looks like valid code that includes all the features we need to have the same functionality as the Kotlin equivalent. As ChatGPT mentioned in the last sentence, it assumes that we have already defined the data schema for our entities. But we don’t— so let’s create them.

We’re using the BookEntity as input for the conversion:

From Kotlin to Rust converted entity model

We copied the code into our project and realized that everything is red — the code is not usable out of the box and the project does not compile.

At arconsis, we have already used ChatGPT for such purposes, so we actually expected this. It’s also a good sign for us (developers) that we will keep our jobs at least a little longer.

But for the sake of completing the experiment, let’s (try to) fix the broken code:

Putting it all together

This is going to be the most interesting part because, as mentioned, we’re trying to use a language that we’ve never used before with some frameworks we don’t know to create something similar to an already existing solution.

This means that we basically lack all the prerequisites we should have when using a programming language, such as

  • The ability to use common programming concepts (variables, mutability, data types, functions, control flow…)
  • Structuring related data (classes and data structures)
  • Managing a growing code base (packages, modules, etc.)

just to name a few.

Without this knowledge, all we can do is rely on the various error messages we get from the compiler and try to fix them manually by applying our knowledge from other programming languages.
This turned out to be harder than we initially thought because Rust behaves and works quite differently than more well-known and taught languages like Kotlin or JavaScript.

One of the various errors thrown by Rust — related to the DB Framework

So this is the part where the experiment seemed to fall apart very quickly:

Without being able to look at the documentation, it’s basically impossible to create a properly working implementation until ChatGPT generates a fully valid and working codebase. It’s just not ready yet to be used by someone without any programming knowledge, which is once again a good thing because it keeps us developers employed (a little bit) longer before chairmen are able to replace us with AI.

However, if you’re familiar with the language and frameworks you’re using, you might be able to create a working project setup and ChatGPT might become quite more helpful. If you’re interested in this topic, feel free to read our article about using ChatGPT to create a portfolio website:

While it’s amazing to see ChatGPT’s ability to convert code snippets from one programming language to another, there’s still a requirement that the developer using the tool has to be able to understand the output received from ChatGPT and use it properly.

Make sure to follow arconsis to not miss any new posts in the future. Thanks for reading!

This article series was written by Markus Lindner and Patrick Jung.

--

--