Live data from Hacker News

C2Rust: translate C into Rust code

c2rust.com

11–20 of 79 posts

Re: C2Rust: translate C into Rust code

#11
post #10
post #5

The insertion sort conversion example makes Rust look intimidating and verbose as compared to C :-).

Fortunately, once you start refactoring the Rust to take advantage of the functionality that doesn't exist in C (like slices, iterators, etc), things start to look much cleaner. Translating the code into ugly, unsafe Rust is intended to only be the first step. We're working on tools to help with that refactoring process, too.

Sounds like a case of "writing C in Rust" compared with "writing Rust". You're not really writing in a language until you're thinking in its idioms.

Re: C2Rust: translate C into Rust code

#14
post #7
post #2

How’s this compare to https://github.com/jameysharp/corrode

(I'm one of the authors of this tool) Our original plan was to simply work to improve Corrode. We eventually decided to to implement a new tool that uses Clang as the frontend in order to get more reliable parsing, preprocessing, and type checking of the input C code. The result is that we are able to support more code and C extensions than we thought we'd be able to building on top of the good work of the Corrode pr…

Is there any hope of going after even a subset of C++? I know the difference is enormous, I'm just asking the question.

Re: C2Rust: translate C into Rust code

#15
post #6
post #2

How’s this compare to https://github.com/jameysharp/corrode

He doesn't appear to be a direct contributor, probably more of an inspiration. The only overlap I know of is that he also works/worked at Galois. c2rust is written mostly in Rust, Python and C where as corrode was written in Haskell. The output of c2rust looks more like a literal projection from C into Rust, Corrode tried to limit the use of unsafe. $ tokei ------------------------------------------------------------…

> The output of c2rust looks more like a literal projection from C into Rust, Corrode tried to limit the use of unsafe.

I don't think either statement is true. Both projects bill themselves as semantics-preserving translators, as in they try to generate Rust code which compiles and works the same way the original C code did (in fact corrode has a cc script which first compiles C to Rust then Rust to native).

Citrus is the literal projection: it's not semantics-preserving and usually doesn't generate compilable code.

Re: C2Rust: translate C into Rust code

#16
post #13

So the demo does actually translate C into unsafe Rust. I'm guessing this translator is, thus, unaware of Rust borrowing/ownership system?

That's right. The translator just tries to preserve the original code. Refactoring things into proper, idiomatic Rust is the next step. The hope is that it will be easier to get the translation correct transforming unsafe Rust to safe Rust than having to go from C to safe Rust.

Re: C2Rust: translate C into Rust code

#17
post #13

So the demo does actually translate C into unsafe Rust. I'm guessing this translator is, thus, unaware of Rust borrowing/ownership system?

> So the demo does actually translate C into unsafe Rust.

It's semantics-preserving, and C constructs are not expected to match safe Rust, so that makes sense, Corrode does more or less the same.

Citrus generates "safe rust", it also doesn't generate working code.

Re: C2Rust: translate C into Rust code

#18
post #11
post #10

Earlier quoted context omitted.

Fortunately, once you start refactoring the Rust to take advantage of the functionality that doesn't exist in C (like slices, iterators, etc), things start to look much cleaner. Translating the code into ugly, unsafe Rust is intended to only be the first step. We're working on tools to help with that refactoring process, too.

Sounds like a case of "writing C in Rust" compared with "writing Rust". You're not really writing in a language until you're thinking in its idioms.

> Sounds like a case of "writing C in Rust" compared with "writing Rust".

Duh? It's literally taking C code and generating Rust code which behaves the same.

> You're not really writing in a language until you're thinking in its idioms.

The entire point is to get your foot in the door.

This gives you a pile of Rust code which (bugs aside) should behave the exact same way C code does.

From there on you're living in the Rust world and can improve that as you see fit.

If you can afford doing the initial transition in one short, later improvements are much simpler than having to maintain an internal (moving) front between remaining C code and new Rust code as e.g. librsvg does.

Re: C2Rust: translate C into Rust code

#19
post #7

Earlier quoted context omitted.

(I'm one of the authors of this tool) Our original plan was to simply work to improve Corrode. We eventually decided to to implement a new tool that uses Clang as the frontend in order to get more reliable parsing, preprocessing, and type checking of the input C code. The result is that we are able to support more code and C extensions than we thought we'd be able to building on top of the good work of the Corrode pr…

Is there any hope of going after even a subset of C++? I know the difference is enormous, I'm just asking the question.

We've only started pondering what subset of C++ we might be able to support in future work for the project. Currently only C is in scope. That's an obvious next goal but it seems quite a bit more daunting!
Post reply on HN