Hacker News new | ask | show | jobs
by jansc 2305 days ago
Heh, I also released my first project in Rust yesterday. An ncurses gopher client. I probably missed many patterns and idioms as well. Starting with Rust is quite hard, even as an experienced developer. I spent hours "fighting" the compiler. Code available at https://github.com/jansc/ncgopher/
1 comments

Could you talk a bit about the kinds of things you had to fight the compiler on?

Can you identify any idioms or approaches that seem normal or natural in other languages, but which are problematic in Rust?

Two examples: The application has two main components, a controller and a struct containing the ui implementation. Callbacks in the cursive library are called with a reference to Cursive, but I had a hard time figuring out how to pass the controller to the callback. In the end I decided to use a message channel (mpsc::Receiver and mpsc::Sender) to let the two components communicate. After some struggling I saw that cursive has a function to set user data which is accessible inside the callbacks. So I pass the channels through userdats. Not sure if this is the best architecture. I never managed to pass a reference to the controller to the ui callbacks. The Compiler complained about undefined lifetime of the reference, though I knew that both outlive the application runtime.

A similar situation occurred in the asynchronous communication with gopher servers. I use a thread to fetch a resource from another server while keeping the ui responsive. Passing the result from the thread back did not work out in the beginning.

I don’t think the idioms are so different in Rust, but when passing data between components, threads and the like, you have to be very specific in your code, using Arcs, RwLocks, Mutexes, etc.

On the bright sight, when the code compiles, it usually worked as intended. Using C or C++, there would be more race conditions, null pointer exceptions and more.