Hacker News new | ask | show | jobs
by nathan_f77 3368 days ago
Are there any automated tools that you can help you convert a C codebase into Rust? And is there any reason why Rust couldn't be as portable as the C code that makes up curl?

Is it very difficult to transpile C into Rust?

As an aside, I not like the curl style guidelines [1]. Especially "'else' on the following line", and "No space before parentheses". That doesn't look nice at all.

[1] https://curl.haxx.se/dev/code-style.html

4 comments

For automated tools that can convert a C codebase to Rust, you're looking for is Corrode: https://github.com/jameysharp/corrode

Last I heard, Corrode can convert many C syntactic structures into their Rust equivalents, they usually compile and sometimes they even work. Even when the conversion works flawlessly, the resulting Rust does exactly what the original C code did—there's no safety benefit (but you can begin refactoring to clean things up).

And is there any reason why Rust couldn't be as portable as the C code that makes up curl?

In theory no, in practice yes. Rust is using LLVM project to generate machine code, and that only supports certain architectures. There are however other safe languages that can generate C, if this is a requirement.

> Is it very difficult to transpile C into Rust?

If safety guarantees could be added to C by some conversion or analysis, there would be no need for Rust to exist.

Rust (and other languages with similar goals) is safer for many reasons, not limited to languages itself. Stdlib and runtime play their part too. Automatic conversion that takes all that into account is either impossible or impractical. There is a tool for converting C into unsafe Rust (corrode), which might be useful for people attempting to port C code, but in itself doesn't improve code safety at all.

https://github.com/jameysharp/corrode is a C-to-Rust translator. It won’t be idiomatic Rust, though; not by a long shot. (Rust is much more restrictive in what it allows, for reasons of safety, though more expressive.) And it may not be correct, either; corrode is getting pretty good, but it’s not perfect yet.