Comparing Rust and D As Better C

Dibyendu Majumdar mobile at majumdar.org.uk
Fri Jan 15 22:59:54 UTC 2021


In my C project (which is a compiler project for a scripting 
language) I have the following design.

I use allocators to manage memory.
Strings encountered during lexing are interned because the 
scripting language needs this feature
Various objects such as ASTs, instructions, constants etc. get 
created, and objects can reference other objects.
At the end of the compilation, everything is discarded by the 
allocators.

Simple and classic way I guess.

I have not yet looked at porting to D as I think I can pretty 
much keep this design in D.

In Rust though there is simply no way for references to be kept 
between data structures. Even if I as a programmer know that the 
references are safe because all memory is managed by allocators 
that have a longer life than the individual objects, there is no 
way to convince Rust that this is the case.

So with Rust I need to use either smart (ref counted) pointers, 
or use indirection (such as integer handles) that are copy-able, 
and so the compiler is happy with those.

I can see why this is the way in Rust. I can also see why Rust 
cannot trust the programmer, and therefore doesn't allow me to 
express the design I have today.

Just thought this might of interest. One implication is that if 
Rust is safe, D can never be safe in the same way. To be safe as 
Rust you'd have to prevent sharing of references completely. 
(oversimplified but broadly speaking)



More information about the Digitalmars-d mailing list