Hacker News new | ask | show | jobs
by klodolph 1748 days ago
The catch is that code is hard to translate into Rust.

"I have this code, you see... and it takes a couple mutable references into an array... how do I translate this into Rust?"

There is no one-size-fits-all answer to that question. The code may be correct in C or C++, but the Rust type system may give you one hell of a hard time proving that it is correct to the Rust type system's satisfaction... so you refactor your code completely, or you use integer indexes into arrays rather than references, or you use unsafe code...

I've written some amount of Rust code at this point. About half of the time, when I write a project in Rust, there comes a point at which I'm fighting with the type system. I feel like this should stop happening, at some point.

1 comments

For sure. The solution has downsides but it does largely solve the problem. It isn't strictly better than go, but it is an important difference.

Mostly off-topic but FWIW "mutable reference into an array" is typically very easy in Rust you just accept a &mut [T]. This also nicely enforces that you don't try to append because that would be wrong 99% of the time.

> Mostly off-topic but FWIW "mutable reference into an array" is typically very easy in Rust you just accept a &mut [T].

I had specifically worded it as "a couple mutable references into an array". How do I take a small number of references into an array, say two or three?

Off the top of my head you do something like https://play.rust-lang.org/?version=stable&mode=debug&editio...

Yeah you gotta write it out per number of times. Luckily this rarely comes up in this specific form.

Wow, that code is as bad as I thought it would be. Yeesh.

> Luckily this rarely comes up in this specific form.

Well, yeah—it would come up rarely because it only solves the problem under very specific circumstances!

Yeah I mean you might be able to make it better, this is just what I would reach for first.

It is so rare that I don’t think I’ve ever seen it in any rust codebase I’ve ever looked at.

Ah, missed that. You are right, in that case you need to start making some decisions depending on the situation.