Newbie Looking for Help on Improving Rust Code Efficiency

Hey there, guys!

I've been programming for twenty years, but am a bona fide Rust newbie looking to make use of Rust's speed, safety, and portability in future projects.

I've just finished porting a simple HTTP file server for Termux on Android over to Rust from Python, so if you could give me some tips to improve my code's efficiency and execution speed before I start tackling larger projects, I would really appreciate it!

I wouldn't worry about speed unless you notice it's too slow. Most the the execution time will be spent on Network IO, so I doubt it matters too much. There are, however, a few stylistic issues you could improve on. I would try to split the code into different files. Especially the CSS and JS code can live in their own file and be included with include_str. I would use different request handlers for thumbnails and deletions (the deletion should also be a POST request).

You can use a POST request, but I think you should use a DELETE request :wink:

1 Like

Usually, the biggest speedup you'll find is by compiling in --release mode. Debug mode, the default, turns off a lot of compiler optimizations that the standard library is designed to exploit.

Thanks for the answers, guys!

include_str!() looks like a great macro to use. I didn't spend too much time optimizing the code structure since this was just a pet project to get my feet wet, but if I were doing this professionally, I would definitely reorganize the code.

The reason why I asked about execution speed is that when I tested on my laptop, the Rust version was 31x faster than the Python version, but on my phone, the Rust version running in release mode was actually 30% slower than the Python version, which defies all logic and expectations.

Now that I've done more testing though, I believe that it's probably more due to my phone's system libraries being strange rather than anything I'm personally doing with my code.

In any case, thanks for all of your time! I'm glad to be a part of this community. :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.