I've mostly been putting functions, structs enums and tests in the same file and it's starting to feel like a mess... I am constantly losing track of which function is where and I'm never quite sure if I have code in the right place. I know this is kind of vague, but that's been my general feeling while working in my largest personal project so far. It's essentially a large Algorithm that has many smaller steps that needs to run in a sequence.

I think I might just have too many loose functions and maybe I should use more impl methods and traits? I'm also thinking I should try the builder pattern in a few spots.

Anyone have any guidance on code organization in rust?

  • bobtreehugger@awful.systems
    ·
    1 year ago

    Functions are fine, don't move to struct impls unless it makes sense (but do if the functions all take the same struct as a param).

    You can go pretty far with modules and functions. Group related functions and move them to new modules. You can also hide functions that are only used inside one of the submodules by just not marking them as pub.

    One thing that comes to mind is that if the steps of your algorithm all take and return the same data, you can have a trait that expresses that (possibly one of the Fn traits if you're going to just use functions), and you can define and rest each step separately.

    It's hard to give more concrete advice without knowing more about your project