Hello
I've been interested with Rust for the last year, but cannot find time to learn Rust.
Coming from Python, i have found my self quite difficult to learn Rust and grasp its concepts.
So, for the last couple of weeks, i have been able to spare some times to learn Rust, and created a mini project to parse Vendor OUI with Rust.
If you would be so kind to spare some of your time to review my code and give me some constructive feedback on how to tackle this, and some guidance for me about what i can improve because its not the rust way of doing things, i would be very happy. I want to improve my skills on Rust so i have another tools in my toolbox
This is not a promotion, because i believe there's another tools just like mine out there nor i want you to use my project, because this project is only for me to test my skill for using Rust.
Thank you in advanced :D
=== Additional text after posting ===
Thank you for those who reply, i really learned something new. I'll try to improve my code :D
Shout Out to Nous, Hades, Kwdg, BB_C
<3
Welcome to Rust, I hope you enjoyed learning and using it :)
One major advice would be to run
cargo clippy, which gives a lot of helpful hints. Apart from that I also have some feedback.These comments should be doc-comments (
///) so thatcargo docpicks them up.Not saying that your return type is bad, but in general you might be interested in crates
thiserrorandanyhowthat simplify error handling.Duration::from_mins(1)would be much more readable.You should almost never panic. Your function already returns a Result<>, so you should be using that for error handling.
Not Rust specific, but usually I recommend to handle errors first, and return from the function early. This way you reduce cognitive load on the reader, and reduce nesting of if-blocks.
There's a convenient method to split in two substrings that you could have used:
I would probably write a regular expression that replaces all contiguous whitespace with a single space instead.
Aren't you trimming this new line off in line 181?
This is somewhat inefficient, since you'll be allocating and copying your string here.
Hello and Thank You :D Rust really intrigues me so i really want to explore more about Rust
Thank you for your input. I'll refactor my code
Conceptually, error handling in Rust is incredibly simple: if a function can fail, its return type is an enum of either the result of the function (in case of success) or a description of the error that happened. This enum is called Result. See:
You don't. You can 100% handle errors without any additional dependencies, and probably you should be doing that in the beginning. The crates simply add a little bit of syntactic sugar to simplify some boilerplate that you'll have to start writing as soon as your error handling gets sufficiently complex.