I just got some code working that detects the user's shell in Windows. The full code is here: Rust program to identify the shell that invoked this application · GitHub
This is a more or less direct translation of the equivalent code from the shellingham
Python library. I wanted something similar in Rust, and I was looking for a non-trivial problem to get some experience trying to write more idiomatic Rust code (I've used very little Rust, being mostly a Python programmer, and it shows ) So I thought I'd see how porting that code would go.
It took me some time, but the code now works. It doesn't seem very clean, though - the explicit state value in my iterator implementation seems clumsy, and my handling of options and errors can best be described as "I found something that worked".
One thing that may look odd is the error handling - I catch and ignore all errors, turning them into "not found". This is deliberate, at least for now, as I want the find_shell
function to have a simple interface, where there's no need for error handling beyond "I couldn't find the answer". Maybe longer term I'll return actual errors properly, but error types and threading Result
values through the code are something I want to leave until I have a bit more experience - I'm having enough trouble handling Option
cleanly!
Any comments would be welcome, particularly on how to make this more idiomatic. I felt like I spent a lot of time looking for ways to do things that seemed like they ought to be simpler (trimming trailing null characters in my implementation of From<PROCESSENTRY32W>
feels particularly clumsy). I feel like that's my Python experience showing - getting stuck on "how I'd do this in Python" and not knowing how to look for the natural Rust approach.