4 ms·
I am wanting to convert about a 5 functions/100 lines of my Python project into Rust. I cobbled together the Maturin integration with my project earlier this we
by abdullahkhalids 3y ago
I am wanting to convert about a 5 functions/100 lines of my Python project into Rust. I cobbled together the Maturin integration with my project earlier this week, which seems to work on some test functions. But I don't know any Rust!
What's the best way to learn enough Rust to do this? My code is basically just some Numpy array manipulation, with some unfortunate for-loops which can't be vectorized, which is the source of the slow speeds.
- aardshark 3y agoOpen up Chat GPT, paste your functions and ask it to convert them to rust. Go through them 1 by 1, see if you understand and ask questions about anything you don't recognise. Don't expect the output to be perfectly logically correct, you will have to ensure that yourself. I've found Chat GPT to be really excellent for quickly getting myself up to speed with languages that I'm not familiar with.
- eigenvalue 3y agoYes, that’s my advice as well. Set up vscode with rust analyzer and paste any errors it shows back into the same ChatGPT conversation and it will debug everything for you.
- abdullahkhalids 3y agoThanks, that sounds good. I will search myself, but is there are a great rust syntax reference doc?
- eigenvalue 3y agoThe syntax isn’t really the hard part, it’s the rules around memory ownership and when you need to clone variables. There are lots of gotchas and the compiler is very unforgiving. If you want to convert your 5 functions into rust in a reasonable time instead of spending weeks grappling with the differences, just use ChatGPT and iterate by pasting in the errors from rust analyzer and the compiler. If you actually want to learn rust then that’s a different story and you should probably check out Steve Klabnik‘s book or something like that (or just look at the sample code I linked to in my other comment from my own recent rust library for python).
- eigenvalue 3y agoYou can see how I did something similar in my library here: https://github.com/Dicklesworthstone/fast_vector_similarity/blob/main/src/lib.rs https://github.com/Dicklesworthstone/fast_vector_similarity/... Basically you use ndarray instead of numpy, try to vectorize anything you can, and for the for loops that can’t be vectorized, you can use rayon to do them in parallel.