Comparing Rust and D As Better C

Dennis dkorpel at gmail.com
Wed Dec 16 00:36:03 UTC 2020


On Tuesday, 15 December 2020 at 23:25:02 UTC, Dibyendu Majumdar 
wrote:
> I am working on porting a C project to Rust and Better C in 
> order to get a real world idea of what the differences are.

Is it a public C project?

> First impressions of Rust vs D As Better C:
>
> Rust has traits - nothing equivalent in the D As Better C 
> subset.
> Another difference is Rust has enumerated types - with D a 
> library solution is possible.
> Rust appears to have a good tooling in VS Code with pretty good 
> IDE that gives you hints, flags errors as you type code, and 
> allows you to lookup docs.

Fair points, but I don't see the link to porting C code. C has no 
traits, C has no enumerated types, and IDE features are not 
needed when doing a simple translation. At least my C to D 
translation work has been very straightforward so far, maybe Rust 
requires more refactoring so then it's handy.

> I suspect porting the code to D subset will be trivial.

There are syntax changes and some stricter rules with implicit 
pointer conversions, integer truncation, and switch case 
fallthrough etc, but there is mostly a 1 to 1 map from C 
constructs to D (betterC) constructs.
The most challenging part is non-trivial uses of the C 
preprocessor. Often there is an easy solution where you can get 
the same behavior with D features (`import` `version()`, `static 
if ()`, `enum`, `mixin template`), but crazy things are possible 
with text macros and you hope to not encounter them.

Also it can be annoying when system headers are used that are not 
in druntime (e.g. regex.h, stdatomic.h), that means you need to 
make them yourself, find an existing binding, or find a 
replacement for their use.

My experience is mostly based on my translations of certain 
single-header C libraries, glfw and libsoundio. I've written some 
notes about the translation of those last two which might 
interest you:

https://github.com/dkorpel/libsoundio-d#translation-events
https://github.com/dkorpel/glfw-d/wiki/Notes-on-the-translation-process

> Porting to Rust is at the moment a challenging task (I am 
> trying to replicate the C code as much as possible, including 
> writing allocators etc.)

I have no experience with writing Rust. I do know there is a 
migration tool for it though:
https://github.com/immunant/c2rust



More information about the Digitalmars-d mailing list