Is it possible to convert C++ code to Rust using a software?

#1

Forgive me for my lack of computer science knowledge but is it possible to convert C++ or C code into Rust using a software?

0 Likes

#2

It’s possible to convert C code to Rust pretty well. There’s minimal support for C++, but it’s not supported in any useful sense.

Note however, that automatically converted C/C++ code has all the unsafety of C/C++ (Rust doesn’t make it safe) and very very ugly syntax.

3 Likes

#3

Instead of converting (I’m assuming a transpiler is being asked about?), consider using bindgen to automate creation of an FFI binding to the underlying C (and some subset of C++) lib.

4 Likes

#4

After converting it to Rust, is it possible to make the code safe and is it possible to make your code not ugly?

0 Likes

#5

Sorry mate I don’t understand some stuff,

What is FFI?

On the website it states that "bindgen automatically generates Rust FFI bindings to C (and some C++) libraries."

So doesn’t this imply that it converts Rust to C/C++?

0 Likes

#6

FFI stands for Foreign Function Interface, which is a fancy way of saying “call code from a library written in another language”.

Not exactly. Bindgen will create function and struct declarations in Rust, allowing your Rust code to call things in an existing C library.

Definitely! If you know what to look for, you’ll see this happening quite often in the Rust ecosystem. For example, large parts of the Rust standard library are just safe wrappers around the system’s C standard library (e.g. std::fs, std::thread, and std::net). There are also crates like ssh2 and git2 which are safe bindings to the libssh2 and libgit2 libraries, respectively.

A lot of the time it’s better to reuse an existing library written in another language than porting the entire thing to Rust. FFI is just a mechanism for allowing programs written in different languages to talk to each other.

1 Like

#7

In the future, it is possible using a software to convert unsafe C/C++ into safe Rust clean code?

0 Likes

#8

That totally depends on if someone wants to write a tool to convert unsafe C/C++ into safe Rust, and if they’ve got a valid use case.

That said, the reason a bit of code might be unsafe depends entirely on context and the various assumptions made by the rest of the codebase. So even though tools exist for converting unsafe C/C++ into unsafe Rust, I’d be a little cautious about a tool which tries to make unsafe code safe in a sound way (otherwise what’s stopping me from just removing unsafe from all the function signatures with a find/replace?).

1 Like

#9

I don’t think so. If it were possible to convert any unsafe C or C++ code into safe Rust code, then any C or C++ code could automatically generate safe applications, and so there would be little reason to use Rust in the first place.

11 Likes

#10

There is work being done on converting C into slightly less ugly, slightly less unsafe Rust code.

There doesn’t exist a straightforward 1:1 mapping between C and safe Rust. Not only C as a language doesn’t map very well (it has void *, NULL, globals, mutable aliasing everywhere, no standard error handling), but also C programs very frequently use programming patterns that are just unsafe (e.g. passing and storing a pointer to a buffer without length of the buffer, and just assuming the buffer is large enough).

Converter of C to safe Rust is almost like a converter of buggy programs to programs without bugs. If only we had such a thing!

8 Likes

#11

… and if only that were true! :wink:

1 Like

#12

This hasn’t seen a commit in a year and a half, but Jamey Sharp up here in Portland was working on corrode for a while.

Edit: Looks like Jamey has written about c2rust vs Corrode recently.

1 Like

#13

No ,I don’t think is a problem .Because the rustlang is simple than C++ (maybe haha~)
and it’s not easy to code in error.

0 Likes