by thesuperbigfrog · 3y ago · 0 comments
https://threadreaderapp.com/thread/1577667445719912450.html
At $WORK we are converting a graphics processing application written in C to Rust and the experience has been similarly enlightening.
Learning Rust has been a process, but the dividends are real and exciting.
Biggest takeaways:
Rust's ownership rules enforce "multiple readers, one writer" patterns so plan for such patterns in your design.
Embrace Rust's Result and Option types for error-handling and design. They are elegant and powerful when used with 'match'.
Unit tests right next to the code they test is a huge win. Doc-tests and 'cargo doc' is literate programming (https://en.wikipedia.org/wiki/Literate_programming).
The phf crate (https://docs.rs/phf/latest/phf/) is awesome for global constant data tables.
>> Was there anything that surprised you?
Our Rust code is much more concise than the previous C code. It takes about three times as much C code versus Rust code, but the C code feels much more brittle and less portable.
Not to say I couldn't have gotten the same education from another language, but something about Rust clicked for me that others didn't.
An example of this is generating a JSON and only learning the correct schema at runtime.
Or connecting to an external database with tables of unknown structures that you can’t type check.
You can, of course deal with all these by wrapping the objects in some structure; but it’s just one more thing you need to keep in mind.