[Absolute Beginner] Is this txt-splitter Rustlike?


#1

Ok, so yesterday I wrote a very small program in Rust that splits up Files for me based on the Beginning of a Line.
(I come from a Dynamics NAV Background and I used this to split up an Export of a Complete Database).

But I am sure I made some mistakes and would like to know what the Rust Community thinks about this:
-http://github.com/fabiovitalba/file_splitter/blob/master/splitter.rs

It took me quite a while to get it to work. I had some C/C++ lessons in School, but I’ve been coding C#, Java and (for the majority of my time) C/AL, so I lost some knowledge about the very low-level-stuff…

Some weird things I did, because it wouldn’t compile otherways:

  • I have to create a tmp.txt file, because otherwise by the time new_f gets used, it could not be initiated.
  • I have no idea when I have to use “”.to_string() or the &… I probabibly have to go through the Tutorial again for this one.
  • Instead of handling the line with invalid utf-8 formatting, I just simply do a unwrap_or("") and replace it with an empty line. This is my biggest concern…

I would be very glad if you guys could just give me some insight on where I should improve and change my code, for it to be cleaner and simpler.

Thanks already!


#2

Hi, and welcome to Rust! You’re definitely not alone with some points that take getting used to.

A few comments:

  • new_name does not need to be scoped to the function.
  • Instead of creating a dummy file, you can use an Option for new_f.
  • Some to_strings are not necessary. For example, instead of a[x..y] == "literal".to_string(), use &a[x..y] == "literal". In general, mismatches between owned (String) and borrowed (&str) types should be resolved by borrowing the owned value (if possible).
  • to_string() is slower than to_owned() or into(), but probably not for much longer.
  • Instead of to_string() I prefer clone() if it’s already a String.
  • For more complex path operations, see std::path.
  • It’s good style to at least add .unwrap() for Result values where the compiler emits warnings. That way, you don’t ignore failures at runtime.

I put a version with these changes here: https://gist.github.com/birkenfeld/fb4ec61ec70f781b93f1


#3

Sorry I didn’t reply sooner…

Thank you for your help!

The part about strings is actually pretty neat. I will try to remember to use the owned value if I have the chance to!

I already checked out your Version and it looks a lot cleaner. I’ll go ahead and make mine a bit more sorted out and dynamic as well. :slightly_smiling:

Thanks again!