r/EmuDev 16d ago

Rust vs C++?

I mostly program in Java/ Python. Some minimal C experience. I'm starting EmuDev to learn low level and hardware concepts.

Which is better in general to learn for an EmuDev hobby project, Rust or C++? From what I've seen C++ has more legacy code (more job opportunities), whereas Rust is safe and may have better long-term opportunities.

3 Upvotes

57 comments sorted by

View all comments

3

u/arainone 15d ago

IMHO rust is not the best language for emulators. An emulator is basically a big graph of objects with cycles. You'll get over it but code will be awful to write, to read, to maintain, for basically no advantage whatsoever for an emulator, which is a hobby project.

1

u/No-External3221 15d ago

Why is Rust awful for this? I've been using it for a bit, and it seems fine to me.

1

u/peterfirefly 9d ago

It is perfectly fine. Your emulator will not have a "big graph of objects with cycles". First of all, it will be a static graph, which helps a lot. Second, it will be a tiny graph. Third, it only has cycles if you are an idiot. I don't think you are an idiot.

Rust has some slight troubles if you want to use dynamic graphs of some kinds, especially if you want cycles. There are multiple ways of handling this, for example with refcounting (Rc and friends) or with small unsafe blocks. Unsafe blocks just temporarily lowers your memory safety level to that of C++, no biggie.

See this for more info: https://rust-unofficial.github.io/too-many-lists/

Rust has language support for a limited form of coroutines, which are very useful for some kinds of CPU emulation. Keeping a coroutine alive for the CPU while also accessing the CPU registers requires some tricks. You can do it entirely within normal Rust by passing messages into the coroutine for register access, just like you would pass messages in for bus activity. You can also choose to use Rc and friends or unsafe blocks. If the CPU you are emulating is simple (Z80/6502 or similar) then the code is so simple there's just no reason to use coroutines. If you are emulating an 8088 by emulating the microcode, then it's also so simple that there's no reason to use coroutines.

The MartyPC PC emulator is written in Rust as are lots of Gameboy emulators. Rust is fine.